How to parse this JSON?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

How to parse this JSON?

Post by wvoyance »

I have a string look like

oo= { "stat":"ok", "list":[{ "url":["http://www.worldcat.org/oclc/61280830?referer=xid"], "publisher":"Shi bao wen hua chu ban qi ye gu fen you xian gong si", "form":["BC"], "lang":"chi", "city":"Taibei Shi", "author":"Liu Li'er.", "ed":"Chu ban.", "year":"2005", "isbn":["9789571342986"], "title":"Dongjing man chi ju le bu", "oclcnum":["61280830", "82307052"]}]}

How can I parse into
publisher
author
etc

My understanding is
oo.publisher = ...
should get the publisher?
Is not it just a list?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to parse this JSON?

Post by requinix »

Is oo the JSON-decoded object or are you still stuck with a string?

Well, once you get it into an object, to get to the publisher information you have to go into the list array.

Code: Select all

   object             array  object
     |                     \/
oo = { "stat":"ok", "list":[{ "url":["http://www.worldcat.org/oclc/61280830?referer=xid"], "publisher":

Code: Select all

oo.list[0].publisher
User avatar
wvoyance
Forum Contributor
Posts: 135
Joined: Tue Apr 17, 2012 8:24 pm

Re: How to parse this JSON?

Post by wvoyance »

Yes, I indeed tried as you wrote. But it does not work. Here is my code:

function FilltheForm(originalRequest){
var textDoc=originalRequest.responseText;
alert(textDoc);
//The alert show the text to be correct
//After adding the following line the innerHTML simply not replaced. No error message found.
publisher=textDoc.list[0].publisher;
document.getElementById("pd0").innerHTML='<textarea name="products_description[1]" cols="70" rows="15">'+textDoc+'</textarea>';
document.getElementById("pd1").innerHTML='<textarea name="products_description[2]" cols="70" rows="15">'+publisher+'</textarea>';
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to parse this JSON?

Post by pickle »

Strictly speaking, you don't have a JSON string, you just have a regular string that looks like JSON. As far as javascript is concerned, you have a String, not a generic Object. You need to convert the string to a generic object. Use JSON.parse() on the string to convert it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply