1

The following string is returned as response from a web request:

[[1367366400000,6435.00],[1370044800000,349004.49],[1372636800000,1178831.00], [1375315200000,2906943.00],[1377993600000,3571615.00],[1380585600000,3852076.00], [1383264000000,3656850.00],[1385856000000,306884.00]]

now when I try to

var data = $.parseJSON(data);

It is giving me an error:

JSON.parse: unexpected end of data error

I also tried:

var data = $.parseJSON(JSON.stringify(data));

It does not give any error, but it also does not convert the text to JSON object.

1
  • 1
    The given response from your web request already evaluates as valid javascript. You could pass it to a variable directly, but you need to evaluate it. var data=eval('[[13...'); Commented Dec 1, 2013 at 20:49

1 Answer 1

4

because the response is not JSON at all, it's a simple array and use eval() to parse it in a new variable.

var myVar=null;
eval('myVar=' + response );
if(myVar)
    console.log(myVar);
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know who downvoted this and why - it is generally valid. Using eval() may not be considered good practice but its still a valid choice?
I don'r care of downvote, yes ! you could check for count of [ and compare theme to count of ] if if first character is: ([) and last character is: (]) and every brace has a match one for closing brace, that's a good choice

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.