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?
Quicksilver
2,7403 gold badges25 silver badges38 bronze badges
4 Answers 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}]'));
answered Nov 15, 2016 at 6:45
Justinas
43.9k5 gold badges72 silver badges108 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
madalinivascu
20 seconds to late :))
You can convert to a valid Javascript object using function
JSON.parse(str);
but string should be in valid json format.
answered Nov 15, 2016 at 6:42
kailash yogeshwar
8341 gold badge9 silver badges28 bronze badges
Comments
try this,
var obj = $.parseJSON(jsonString);
or
var obj = jQuery.parseJSON(jsonString);
may be this might help
answered Nov 15, 2016 at 6:45
Rehan Shikkalgar
1,0571 gold badge8 silver badges17 bronze badges
2 Comments
Justinas
What's the difference between two lines of code? When it's better to use first over second?
Rehan Shikkalgar
'$' and 'jQuery' both are same, except '$' is just an alias of 'jQuery'.
you can convert it to json.parse:
var json = $.parseJSON(myString)
or you can refer this link below
answered Nov 15, 2016 at 6:48
SURJEET SINGH Bisht
88110 silver badges16 bronze badges
Comments
lang-js
new Date(2015,9,25)part of your string? If it is, it will throw error.