Page 1 of 1

How to parse this JSON?

Posted: Tue Apr 24, 2012 11:08 am
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?

Re: How to parse this JSON?

Posted: Tue Apr 24, 2012 11:12 am
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

Re: How to parse this JSON?

Posted: Tue Apr 24, 2012 12:24 pm
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>';
}

Re: How to parse this JSON?

Posted: Tue Apr 24, 2012 12:36 pm
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.