Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What about using array-syntax :</p> <pre><code>jsonObject.query.pages[18978754] </code></pre> <p>Seems to be working, using firebug :</p> <pre><code>&gt;&gt;&gt; data.query.pages[18978754] Object pageid=18978754 ns=0 title=Apple revisions=[1] </code></pre> <p>And :</p> <pre><code>&gt;&gt;&gt; data.query.pages[18978754].title "Apple" </code></pre> <p>Note accessing data-object with an array-syntax is also possible for the other properties ; for instance :</p> <pre><code>&gt;&gt;&gt; data['query'].pages[18978754].title "Apple" </code></pre> <p>That's perfectly valid JS syntax <strong>:-)</strong></p> <hr> <p><strong>Added after seing the comment / edit</strong></p> <p>If you don't know the ids of the pages, you can iterate over the pages, with something like this :</p> <pre><code>for (var pageId in data.query.pages) { if (data.query.pages.hasOwnProperty(pageId)) { console.log(data.query.pages[pageId].title); } } </code></pre> <p>Note that I'm using <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty" rel="noreferrer"><code>hasOwnProperty</code></a> to be sure the object I'm on has the property, and that it's not coming from any kind of inheritance or anything like that :</p> <blockquote> <p>Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.</p> </blockquote> <p>Depending on what's in "<code>revision</code>", you might have to do the same on that one too, btw...</p> <p><br> Hope this helps better <strong>:-)</strong></p> <p><br></p> <hr> <p><strong>Second edit, after second set of comments :</strong></p> <p>Well, going a bit farther (didn't think you meant it literally) : </p> <pre><code>data.query.pages[pageId].revisions </code></pre> <p>is an array (note the <code>[]</code> symbols) that seems to be able to contain several objects. <br>so, you can get the first one of those this way :</p> <pre><code>data.query.pages[pageId].revisions[0] </code></pre> <p>The second one this way :</p> <pre><code>data.query.pages[pageId].revisions[1] </code></pre> <p><em>(there is no second one in the example you provided, btw -- so this is in theory ^^ )</em></p> <p>And so on.</p> <p><br> To get everyone of those objects, you'd have to do some kind of loop, like this :</p> <pre><code>var num_revisions = data.query.pages[pageId].revisions.length; var i; for (i=0 ; i&lt;num_revisions ; i++) { console.log(data.query.pages[pageId].revisions[i]); } </code></pre> <p>And now, inside that loop, you should be able to get the '*' property of the given object :</p> <pre><code>data.query.pages[pageId].revisions[i]['*'] </code></pre> <p><br> So, the final code becomes :</p> <pre><code>for (var pageId in data.query.pages) { if (data.query.pages.hasOwnProperty(pageId)) { var num_revisions = data.query.pages[pageId].revisions.length; var i; for (i=0 ; i&lt;num_revisions ; i++) { console.log(data.query.pages[pageId].revisions[i]['*']); } } } </code></pre> <p>Using this code in firebug, I now get the literral sting you're looking for :</p> <pre><code>Something..... </code></pre> <p><br></p> <p>Of course, you could probably just use :</p> <pre><code>for (var pageId in data.query.pages) { if (data.query.pages.hasOwnProperty(pageId)) { console.log(data.query.pages[pageId].revisions[0]['*']); } } </code></pre> <p>Which will work fine if you always want to deal with only the first element of the <code>revisions</code> array.</p> <p><br> Just beware : in your example, there was only one revision ; the code I provided should be able to deal with many ; up to you to determine what you want to do with those ;-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload