2

I'm trying to parse a JSON string but when I do I get undefined.

var codes = jQuery.parseJSON(response);

$.each(codes, function (key, value) {
  alert(value.Display);
});

Here is the contents of codes variable above:

["{ Display = string1, Sell = string2 }", 
 "{ Display = string1, Sell = string2 }"]

alert returns value.Display as undefined. I expected "String1". What am I doing wrong?

2
  • 3
    use jsonlint.com to validate JSON Commented Jan 6, 2013 at 15:42
  • Your parsed codes value should be like this: [{ "Display" : "string1", "Sell" : "string2" }, { "Display" : "string1", "Sell" : "string2" }]. Commented Jan 6, 2013 at 15:46

4 Answers 4

6

That's not a valid JSON string.
A correct string would look like this:

'{ "Display": "string1", "Sell": "string2" }'
Sign up to request clarification or add additional context in comments.

Comments

3

You can't. There is no Display property in the array, it's an array containing two strings.

The strings are similar to JSON, but not enough to be parsed.

If you make the strings follow the JSON standard, you can parse each item in the array into an object, then you can access the Display property:

var response = '["{ \\"Display\\": \\"string1\\", \\"Sell\\": \\"string2\\" }", "{ \\"Display\\": \\"string1\\", \\"Sell\\": \\"string2\\" }"]';

var codes = jQuery.parseJSON(response);

$.each(codes, function (key, value) {
  var obj = jQuery.parseJSON(value);
  alert(obj.Display);
});

Demo: http://jsfiddle.net/Guffa/wHjWf/

Alternatively, you can make the entire input follow the JSON standard, so that you can parse it into an array of objects:

var response = '[{ "Display": "string1", "Sell": "string2" }, { "Display": "string1", "Sell": "string2" }]';

var codes = jQuery.parseJSON(response);
console.log(codes);
$.each(codes, function (key, value) {
  alert(value.Display);
});

Demo: http://jsfiddle.net/Guffa/wHjWf/1/

1 Comment

saved me alot today. I didn't understand why I had to do that double parse until just now. Thanks for explaining!
1

I got this error by accidentally json-encoding my array data twice.

Like this:

$twicefail = '{"penguins" : "flipper"}';
return json_encode( $twicefail );

Then in my view, I picked it up like this:

var json_data = jQuery.parseJSON(my_json_response);

alert(json_data.penguins);      //Here json_data.penguins is undefined because I
                                //json_encoded stuff that was already json. 

Corrected code follows:

$twicefail = '{"penguins" : "flipper"}';
return $twicefail;

Then in my view, pick it up like this:

var json_data = jQuery.parseJSON(my_json_response);

alert(json_data.penguins);     //json_data.penguins has value 'flipper'.

Comments

0

You can easy try this:

var json = jQuery.parseJSON(response);
//get value from response of json
var Display = json[0].Display;

All you need is just the [0] for specify of data because this is default parameter (unset).
It works for me!

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.