最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert string to object array in jquery - Stack Overflow

programmeradmin4浏览0评论

I have a string as shown below

[{ date: new Date(2015,9,25), NAV: 12},{ date: new Date(2016,9,25), NAV: 22}]

how can i convert that to object array using jQuery?

I have a string as shown below

[{ date: new Date(2015,9,25), NAV: 12},{ date: new Date(2016,9,25), NAV: 22}]

how can i convert that to object array using jQuery?

Share Improve this question edited Nov 15, 2016 at 7:10 Quicksilver 2,7203 gold badges24 silver badges38 bronze badges asked Nov 15, 2016 at 6:39 AjaiAjai 111 silver badge4 bronze badges 10
  • 5 It does not look like a string – Aniket Sahrawat Commented Nov 15, 2016 at 6:40
  • 1 use JSON.pase(yourArray) – M14 Commented Nov 15, 2016 at 6:40
  • Is new Date(2015,9,25) part of your string? If it is, it will throw error. – sam Commented Nov 15, 2016 at 6:43
  • JSON.parse ;) it is – kwoxer Commented Nov 15, 2016 at 6:43
  • Its not string its json array but not properly formatted – Bharat Commented Nov 15, 2016 at 6:44
 |  Show 5 more ments

4 Answers 4

Reset to default 4

Since new Date(2015,9,25) is not valid JSON, than you need to use eval(yourString) to parse your string to valid Object:

console.log(eval('[{ date: new Date(2015,9,25), NAV: 12},{ date: new Date(2016,9,25), NAV: 22}]'));

You can convert to a valid Javascript object using function

JSON.parse(str);

but string should be in valid json format.

try this,

var obj = $.parseJSON(jsonString);

or 

var obj = jQuery.parseJSON(jsonString);

may be this might help

you can convert it to json.parse:

    var json = $.parseJSON(myString)

or you can refer this link below

Safely turning a JSON string into an object

发布评论

评论列表(0)

  1. 暂无评论