32

I have a string in my db I want to pull into my page and convert to a JavaScript object.

[
{id: 1,title: "Long Event",
 start: new Date(2009, 5, 6, 14, 0),end: new Date(2009, 5, 11)},
{id: 2,title: "Repeating Event",
 start: new Date(2009, 5, 2)},
{id: 3,title: "Meeting",
 start: new Date(2009, 5, 20, 9, 0)},
{id: 4,title: "Click for Facebook",
 start: new Date(2009, 5, 27, 16),end: new Date(2009, 5, 29),
 url: "http://facebook.com/"}
]

How can I do this using jQuery?

BoltClock
729k165 gold badges1.4k silver badges1.4k bronze badges
asked May 28, 2009 at 15:12
1

7 Answers 7

64

As of jQuery 1.4.1 you can do this natively

jQuery.parseJSON

See jQuery documentation.

Stacked
7,3767 gold badges63 silver badges76 bronze badges
answered Jul 27, 2010 at 2:36
Sign up to request clarification or add additional context in comments.

1 Comment

Perfectly answers the OP's question.
7

The "official" json2.js script includes 2 methods: one that will safely parse any JSON string to an object (JSON.parse), and one that will convert an object to a JSON string (JSON.stringify)

The script can be found here.

In my post above, I suggested eval(), but there is actually a slightly better way to evaluate JSON (if you don't want to use the json2.js script):

var obj = (new Function("return " + json))();

using the json2.js script:

var obj = JSON.parse(json);
Stacked
7,3767 gold badges63 silver badges76 bronze badges
answered May 30, 2009 at 8:46

1 Comment

Most browsers include a native JSON object these days so there's no need to include json2.js any more. Just use JSON.parse(string) or JSON.stringify(object)
2

how about eval() ?

var obj = eval(jsonString);

answered May 28, 2009 at 15:14

3 Comments

Usually it's not a very good idea to use eval, unless there's absolutely no other way to achieve the objective at hand.
You have a point, but the risk is minimal. Most of the time, you're evaluating JSON generated by your own server code, which is never a security risk?. Even jQuery does calls eval() to convert ajax JSON to an object.
Simplest solutions is best solution. If you know the source of JSON you are parsing, I think eval would be the best choice.
1

Take a look at JQuery-json plugin

var thing = {plugin: 'jquery-json', version: 1.3};
var encoded = $.toJSON(thing); //'{"plugin": "jquery-json", "version": 1.3}'
var name = $.evalJSON(encoded).plugin; //"jquery-json"
var version = $.evalJSON(encoded).version; // 1.3
answered May 28, 2009 at 15:15

Comments

1

Did you look at the jquery-json plugin?

answered May 28, 2009 at 15:16

Comments

0

use

jQuery.getJSON(url, data, callback)

or pass "json" as the type parameter:

jQuery.get( url, data, callback, type )

same applies to:

jQuery.post( url, data, callback, type )

*all in case you are fetching the "string" from an ajax request

answered May 28, 2009 at 15:17

Comments

-1

While you can use the the eval command, you need to check it for safety first. I use:

var data = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.
 test(source.replace(/"(\\.|[^"\\])*"/g, ''))) 
 && eval('(' + source + ')');

That should work (it's been adjusted a bit from the original). The key point is that the JSON string is checked to prevent functions and other actual executable code from sneaking through. (The first regex is the important bit).

That said, the JSON plugin is very good.

answered May 30, 2009 at 8:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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