That works and :-) thank you so much. On Fri, Oct 9, 2009 at 6:00 AM, Chippo <mathew.chap...@gmail.com> wrote:
> > Hi Thai > > you could try something like this I think... > var obj = {'title':'mytitle','etc':'etc'}; > for(var a in obj){ > // a would be the key ie 'title' > // obj[a] would get the value ie 'mytitle' > } > I think that will work I havent tried it myself though. > > > On Oct 9, 5:38 am, Thai Dang Vu <tdan...@gmail.com> wrote: > > Let me rephrase my problem. > > > > $.post('a.htm', $('#formId').serialize(), function(data) { > > // data is a json string > > // I don't know anything about the keys used in that json string, > i.e. > > that string can be > > // '{"name": "James", "age": 27}' or can be '{"title": "jQuery", > "year": > > 2009, "month": 10}' > > var jsonObject = JSON.parse(data); // JSON is fromhttp:// > www.json.org/json2.js > > // because the keys are unknown, I cannot write something like > > jsonObject.name, jsonObject.title ... > > // if the Prototype library is used, I can do like this > > $H(jsonObject).each(function(item) { > > log.debug(item.key + ' ' + item.value); > > }); > > > > }); > > > > If the server returns '{"name": "James", "age": 27}', that block of code > > should print out > > name James > > age 27 > > If the server returns '{"title": "jQuery", "year": 2009, "month": 10}', > that > > block of code should print > > title jQuery > > year 2009 > > month 10 > > > > Thanks for any help. > > > > On Thu, Oct 8, 2009 at 8:01 PM, Michael Geary <m...@mg.to> wrote: > > > I assume that what you mean is you have the actual JavaScript *object*, > not > > > a JSON string that represents the object, right? > > > > > If you had a JSON string, you could just look at the string to see its > keys > > > and values. Or you could paste it intowww.jsonlint.comto pretty-print > > > it. > > > > > If you have a JavaScript object, one easy way to view it is to load > your > > > page in Firefox with Firebug installed, and then add this line to your > code: > > > > > console.dir( yourObject ); > > > > > -Mike > > > > > On Thu, Oct 8, 2009 at 11:20 AM, Thai Dang Vu <tdan...@gmail.com> > wrote: > > > > >> Hi everybody, > > > > >> Supposed that I have a json object but I don't know anything about its > > >> keys and values. Is there anyway to print out all the key and value > pairs in > > >> that object? > > > > >> Thanks. >