1

I'm getting a string which represents a JSON Object (it's a valid JSON string according to JSONLint).

When I call :

console.log(JSON.parse(datasource_graphe_un));

It returns a string, confirmed by :

console.log(typeof jQuery.parseJSON(datasource_graphe_un));

returning string.

Here's an example of the string I try to parse :

[ {"id":195197,"val":9800,"date":"2009/05/11","ax":1242023901,"number":8,"valval":9200} , ... ]

Why can't I parse it ? And why jQuery.parseJSON returns a string ?

Thank's

14
  • If that example is the actual return value, you have an array, not JSON. What is JSON.parse actually returning? Commented Jun 27, 2013 at 14:27
  • It returns an array containing an object for me. cl.ly/Pvkj Commented Jun 27, 2013 at 14:29
  • I can't reproduce your problem. Can you build a fiddle ? Commented Jun 27, 2013 at 14:29
  • 11
    Are you sure your didn't stringify it twice ? Commented Jun 27, 2013 at 14:30
  • 1
    Alright it works... I don't know why but when I try : JSON.parse(JSON.parse(datasource_graphe_un); it returns the correct object (and yes it is twice). Don't know why. Commented Jun 27, 2013 at 15:13

2 Answers 2

2

Yep, probably twice encoding (e.g. you use JSON.stringify() and then encodeURIcomponent()).

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

Comments

1

Using Chrome's Console, this line works fine for me. You may have to iterate through your array if you are trying to parse several results being stored in the variable datasource_graphe_un.

  var k = $.parseJSON('{"id":195197,"val":9800,"date":"2009/05/11","ax":1242023901,"number":8,"valval":9200}');

1 Comment

This should have been a comment. But this has already been said in comments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.