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?

10
  • 5
    It does not look like a string Commented Nov 15, 2016 at 6:40
  • 1
    use JSON.pase(yourArray) Commented Nov 15, 2016 at 6:40
  • Is new Date(2015,9,25) part of your string? If it is, it will throw error. Commented Nov 15, 2016 at 6:43
  • JSON.parse ;) it is Commented Nov 15, 2016 at 6:43
  • Its not string its json array but not properly formatted Commented Nov 15, 2016 at 6:44

4 Answers 4

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}]'));

Sign up to request clarification or add additional context in comments.

1 Comment

20 seconds to late :))
1

You can convert to a valid Javascript object using function

JSON.parse(str);

but string should be in valid json format.

Comments

0

try this,

var obj = $.parseJSON(jsonString);

or 

var obj = jQuery.parseJSON(jsonString);

may be this might help

2 Comments

What's the difference between two lines of code? When it's better to use first over second?
'$' and 'jQuery' both are same, except '$' is just an alias of 'jQuery'.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.