Skip to content Skip to sidebar Skip to footer

How To Parse Json In Javascript To Take Value

I am really stuck in parsing a JSON string and take it's values. I got the JSON string as {'user':{'id':'1','firstname':'Freelogin','created':'0000-00-00 00:00:00','lastname':'Adm

Solution 1:

You should use jQuery.parseJSON. It will use native JSON if available, and only use eval if necessary, after a sanity check.

Solution 2:

Use JSON.parse (redirected from http://json.org), alternatively MDN

Solution 3:

Json is already some javascript. so parsing is just using eval

like:

var foobar = eval(yourjson);
 alert(foobar.user);

Also jquery has some function for it jquery.parseJSON

like:

var foobar = $.parseJSON(yourjson);

Jquery is better because it would make some checks and perform better.

Solution 4:

First, download jQuery.

Second, include it in your page.

Third, if your variable is this:

var jsonString = '{"user":{"id":"1","firstname":"Freelogin","created":"0000-00-00 00:00:00","lastname":"Administrator","email":"fred@websecurify.com", "usergroup_id":"1","status":"1","ip_enable":"N","priv":"0","expire":""},"data":{ "1":{"5":{"last_update":"2010-12-13 16:16:16","status":"0"},"3":{"last_update":"2010-12-13 16:41:48","status":"1"}},"2":{"6":{"last_update":"2010-12-13 16:41:48","status":"1"}}},"server_array":[{"id":"1","name":"anes.yyy.net"},{ "id":"2","name":"neseema.xxx.net"}],"service_array":[{"id":"5","name":"POP3"}, {"id":"6","name":"Cpanel"},{"id":"3","name":"SMTP"}],"sort_by":"servername", "sort_order":"ASC","pagelinks":"","totrows":"2","offset":"0","limitvalue":"10", "rows_monitor":2,"current":"monitor","uri":false}';

then,

var parsedJson = jQuery.parseJSON(jsonString);

will give you the desired parsed object that's ready for manipulation.

I tried out your JSON string on JSONLint and it says it's valid, so you should have no problems with it.

Solution 5:

you probably got your json in som String variable

var json = '{"user":{"id":"1","firstname":"Freelogin","created":"0000-00-00 00:00:00","lastname":"Administrator","email":"fred@websecurify.com", "usergroup_id":"1","status":"1","ip_enable":"N","priv":"0","expire":""},"data":{ "1":{"5":{"last_update":"2010-12-13 16:16:16","status":"0"},"3":{"last_update":"2010-12-13 16:41:48","status":"1"}},"2":{"6":{"last_update":"2010-12-13 16:41:48","status":"1"}}},"server_array":[{"id":"1","name":"anes.yyy.net"},{ "id":"2","name":"neseema.xxx.net"}],"service_array":[{"id":"5","name":"POP3"}, {"id":"6","name":"Cpanel"},{"id":"3","name":"SMTP"}],"sort_by":"servername", "sort_order":"ASC","pagelinks":"","totrows":"2","offset":"0","limitvalue":"10", "rows_monitor":2,"current":"monitor","uri":false}';

now you can easily parse it via jQuery (you also can parse it via native javaScript eval, but there are some security issues, badly formated input string f.e., that is covered with jQuery and not in eval)

result = jQuery.parseJSON(json);

Now you can easily acces your json object

alert('Hello user, your name is ' + json.user.firstname);

Post a Comment for "How To Parse Json In Javascript To Take Value"