views:

91

answers:

3

I've built some JSON

{
    "News": {
         "Article": {"title": "test"},
         "Article": { "title": "test" },
         /*Snipped*/
         "Article": { "title": "test" },
         "Article": { "title": "test" }
    },
    "count": "20"
}

which validates in a JSON formatter http://jsonformatter.curiousconcept.com/. But when I try to ingest this data through jQuery I don't get what's expected:

$.getJSON('php/json.php', function(data) {
  console.log(data);
});

Results

News: Object
Article: Object
title: "test"
__proto__: Object
__proto__: Object
count: "20"

but where are my 19 other Article Objects? I'm new to JSON and jquery's getJSON. Is there anything I'm missing?

+7  A: 

JavaScript objects are dictionaries, which means that the name must be unique. You're replicating Article, so each instance overwrites the previous (or is ignored; I don't know which path the parser takes).

To fix, you need to define this name as referencing an array:

{ "News": { "Article": [
                { "title": "test" }, 
                { "title": "test" }, 
                ...], 
             "count": "20" }

But you could probably reduce this further, assuming that all news items are articles, because count becomes superfluous:

{ "News": [
          { "title": "test" }, 
          { "title": "test" }, 
          ...] }
Anon
Thanks for the thorough explanation!
TravisKs
A: 

You defined the Article-key multiple times. This is not a syntax error (which is why the formatter does not complain) but rather a logical one. You should use an array instead.

elusive
A javascript array is the same as an object - and anyway, if you use the same key for each value in your array you're going to end up with the same problem...
calumbrodie
A JavaScript array is _not_ the same as an Object. Its similar, yes. But it is not the same! An array can only have numeric keys. Additionally, you cannot specify the keys in an array literal (e.g. `array = ['valueA', 'valueB', 'valueC']`). You will end up with the same problem only if you add your values once at a time by using `array[0] = 'someValue'`, which is not the case here.
elusive
+1 @elusive, Array's are zero based integer keys only, where Objects / HSIN Objects are hash based, meaning the index can be a string or numeric.
RobertPitt
+1  A: 

A better format would be

{
    "News": {
         "1": {"title": "test","type":"article"},
         "2": {"title": "test","type":"article"},
         /*Snipped*/
         "19": {"title": "test","type":"article"},
         "20": {"title": "test","type":"article"}
    },
    "count": "20"
}

So you can do with each.

$.each(Object.news,function(id,ObjectItem){
    if(ObjectItem.type == 'article')
    {
        //do something with ObjectItem.title
    }
})
RobertPitt
If you're just going to use numbers, why not a list? The event id can be contained inside the object within that.
Shane Reustle
a `list` ? are you referring to an array ?, if so, then an UID should be the sole owner of an object so that the `{"title": "test","type":"article"}` is purely unique, the id would usually come from a DB record, witch you then can assign to html elements such as `id="article-ID"`
RobertPitt