bammab
bammab

Reputation: 2563

Is it possible to parse JSON with javascript?

I have an external URL for a JSON file which is hosted on another domain (not mine). Is it possible to parse this information with javascript only? Here is a sample of the JSON data. I only want to get "q" values.

[{"url":"http://website.com/?q=who+is+ip+search","q":"who is ip search"},{"url":"http://website.com/?q=eclipse+visual+editor","q":"eclipse visual editor"},{"url":"http://website.com/?q=partition+recovery","q":"partition recovery"},{"url":"http://www.website.com/?q=katzenfurz","q":"katzenfurz"},{"url":"http://website.com/?q=rtfm","q":"rtfm"},{"url":"http://website.com/?q=Google+ist+Dein+Freund","q":"Google ist Dein Freund"}]

Upvotes: 3

Views: 2965

Answers (3)

Manse
Manse

Reputation: 38147

Browsers have native parsing methods -> JSON.parse() and JSON.stringify()

There are also several libraries that add the ability to parse JSON ...

Eval is sometimes used directly within JavaScript - but there are often security concerns when using this method -> http://en.wikipedia.org/wiki/JSON#JavaScript_eval.28.29

Upvotes: 6

user900360
user900360

Reputation:

JSON you know is JavaScript object; yes you can parse it in JS. Though as you have remote server as data publisher, you have to configure that server for a callback function. To make the remote request, you insert a new script tag into your page, which will allow you to specify a remote URL. The reponse back will load a JSON object as a parameter of the callback function you specified in the request.

Once read somewhere. Hope it helped.

Upvotes: 0

Šime Vidas
Šime Vidas

Reputation: 185933

Yes, there is a built-in JSON.parse() function. Just pass the string to the function.

var obj = JSON.parse( data );

Live demo: http://jsfiddle.net/h4XTP/

Upvotes: 0

Related Questions