1

This will work :

var json = { 'items': [{ 'id': 72, 'quantity': 1, 'format': '90ml' }, { 'id': 72, 'quantity': 4, 'format': '70ml'}] }
alert(json.items[1].id);

This will NOT work :

var json = $.cookie('json_string');
alert(json.items[1].id);

the json_string in the cookie is EXACTLY the same as in example #1. After fooling around, I realized I need to do this to make it work:

    eval("var json = " + $.cookie("json_cart"));

Is this the correct way to do it? It seems like a hack, im facing some challenges when trying to make this javascript / c# json communication, i tought it would be very easy. Anyway. At least I got it running now.

2
  • 1
    No, the json_string in the cookie is a string. In example #1 it's an object literal. Commented Dec 3, 2010 at 14:28
  • This question is similar to: How to parse JSON data with jQuery / JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 12, 2024 at 10:49

5 Answers 5

12

This is NORMAL

To de-serialize the string to an object, and assuming you target modern browsers, you could use the native function to do:

var myObj = JSON.parse($.cookie('json_string'));

Or you could import json2.js from the official JSON website and use the same line as above.

Or, as mentioned by subhaze, you can be completely jQuery-y and use:

var myObj = jQuery.parseJSON('{"name":"John"}');

A Note About URL-Encoding

If the string is encoded in the cookie, then you need to decode it first, using decodeURIComponent() like this:

var myObj = JSON.parse(decodeURIComponent($.cookie('json_string')))

A Note About Quoted Keys

Your JSON is actually invalid, as it requires double-quoted keys. You should use double-quotes (") to wrap around your keys and your string values.

Check It

To quickly check for the validity of your JSON, try JSONLint.


Your question is very similar to others. So, in the future, be sure to check for similar questions first by checking the JSON tag or searching for your issue.

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

10 Comments

+1, except that I'd say the second case is a string containing JSON - JSON string seems to imply that it is a special type of string.
umm i get Unexpected token ILLEGAL when using your example
@Skilldrick: Thanks for bringing that to my attention. Fixed.
@Etienne Dupuis: what browser?
@Etienne Dupuis: Works fine for me. Chrome has native support for JSON.parse and JSON.stringify. Do you have jQuery?
|
4

Notice

For either of these two methods to work your String must be written as valid JSON.

In your example you need to wrap your keys in double-quotes " not single '.

{"key":value}

It looks as though you're using jQuery, if so. This should work.

jQuery: Live example

var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

Example obtained from jQuery parseJson


As haylem stated, if you're not using a library json2.js would work:

json2: Live example

var obj = JSON.parse('{"name":"John"}');
alert( obj.name === "John" );

3 Comments

{'items':[{'id':72,'quantity':1,'format':'90ml'},{'id':72,'quantity':4,'format':'70ml'}] } results in : Invalid JSON:
you need to make you ' single quotes " doubles for it to be a valid JSON object jsfiddle.net/subhaze/E2S7K
awesome! in plain JS you can use ' or if it's not a reserved-word no ' or " if it's structured as a valid variable name and JS will accept it. However, when using parsers they're strict on how the JSON is formed so you have to make sure all your keys are wrapped in " for it to consider it well formed and parse it.
2

No. Use json2.js.

Comments

0

You should parse the content of your cookie. Poor man's solution says to eval() it, since json is valid javascript syntax. Good developer's solutions says to use a library to parse json :)

Comments

0

I see you're using jQuery. Don't use eval because it can be unsafe; use $.parseJSON.

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.