1

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":"Administrator","email":"[email protected]", "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}

How to Parse this and take the Results for further processing in JavaScript

6 Answers 6

2

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

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

1 Comment

Although a great solution (with a lot of overhead) in 2010, this should be avoided now.
2

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

1 Comment

This is the most correct answer. It is available in all current browsers (including IE 8+).
1

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.

1 Comment

eval is insecure, and often slower than native parse.
1

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":"[email protected]", "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.

1 Comment

Your example (and possibly the question) is wrong. jsonString is not JSON, or a string. It's just a JavaScript object.
0

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":"[email protected]", "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);

Comments

0

You don't need jQuery, in ECMAScript5 JSON object will be supported natively and with it you can use JSON.parse method to parse a string into a JS object. IE9 will support ES5 and FF and Chrome already do.

For the moment you can use json2.js (you can look at the source here) as fallback for the browsers that don't support JSON natively.

1 Comment

IE 8 also supports JSON.parse and JSON.stringify.

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.