extracting data from a JSON feed

R

rob

Hi,

I want to read data from wikipedia that are passed using a JSON
feed.The data in question have the following structure:

<api>
<query-continue>
<search sroffset="10" />
</query-continue>
<query>
<search>
<p ns="0" title="Wikipedia" />
<p ns="0" title="French Wikipedia" />
<p ns="0" title="German Wikipedia" />
<p ns="0" title="Criticism of Wikipedia" />
<p ns="0" title="2004" />
<p ns="0" title="2005" />
<p ns="0" title="Japanese Wikipedia" />
<p ns="0" title="Spanish Wikipedia" />
<p ns="0" title="History of Wikipedia" />
<p ns="0" title="2007" />
</search>
</query>
</api>

To read the data I use a callback function and am able to see that
there are objects inside (like below), but can't seem to find a way to
read all of their contents.

query=[object Object]
query-continue=[object Object]

I will appreciate any suggestions on how to read all contents of the
JSON feed.

Regards,
Rob
 
G

Gabriel Gilini

Hi,

I want to read data from wikipedia that are passed using a JSON
feed.The data in question have the following structure:

<api>
  <query-continue>
    <search sroffset="10" />
  </query-continue>
  <query>
    <search>
      <p ns="0" title="Wikipedia" />
      <p ns="0" title="French Wikipedia" />
      <p ns="0" title="German Wikipedia" />
      <p ns="0" title="Criticism of Wikipedia" />
      <p ns="0" title="2004" />
      <p ns="0" title="2005" />
      <p ns="0" title="Japanese Wikipedia" />
      <p ns="0" title="Spanish Wikipedia" />
      <p ns="0" title="History of Wikipedia" />
      <p ns="0" title="2007" />
    </search>
  </query>
</api>

That's obviously not JSON[1], but probably XML.
To read the data I use a callback function and am able to see that
there are objects inside (like below), but can't seem to find a way to
read all of their contents.

If you don't know how to iterate through an Object's properties[2],
there's a lot you should learn before you start writing production
code.
Please refer to the language specification[3] or go through some
beginner's guide, like Mozilla's[4].

[snip]
I will appreciate any suggestions on how to read all contents of the
JSON feed.

You'll be better learning it yourself.

[1] http://www.json.org/
[2] https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Objects
[3] http://www.ecma-international.org/publications/standards/Ecma-262.htm
[4] https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide

Cheers
 
R

rob

I want to read data from wikipedia that are passed using a JSON
feed.The data in question have the following structure:
<api>
  <query-continue>
    <search sroffset="10" />
  </query-continue>
  <query>
    <search>
      <p ns="0" title="Wikipedia" />
      <p ns="0" title="French Wikipedia" />
      <p ns="0" title="German Wikipedia" />
      <p ns="0" title="Criticism of Wikipedia" />
      <p ns="0" title="2004" />
      <p ns="0" title="2005" />
      <p ns="0" title="Japanese Wikipedia" />
      <p ns="0" title="Spanish Wikipedia" />
      <p ns="0" title="History of Wikipedia" />
      <p ns="0" title="2007" />
    </search>
  </query>
</api>

That's obviously not JSON[1], but probably XML.
To read the data I use a callback function and am able to see that
there are objects inside (like below), but can't seem to find a way to
read all of their contents.

If you don't know how to iterate through an Object's properties[2],
there's a lot you should learn before you start writing production
code.
Please refer to the language specification[3] or go through some
beginner's guide, like Mozilla's[4].

[snip]
I will appreciate any suggestions on how to read all contents of the
JSON feed.

You'll be better learning it yourself.

[1]http://www.json.org/
[2]https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_wi....
[3]http://www.ecma-international.org/publications/standards/Ecma-262.htm
[4]https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide

Cheers

Obviously the sample data presented is not JSON, but reflects the
structure of the dataset.
I found the solution to my problem which is something like this:

var queryObj=JSONData.query.search;

for (var key in queryObj) {
var obj = queryObj[key];
for (var prop in obj) {
alert(prop + " = " + obj[prop]); }}

Best,
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top