1

I wan't to know how store multiple entities in json file (Structure) I will want to find by id functions (JQuery/javascript) and easy sorting (0,1,2...200).

Here my code:

{ 
    "id" : 5,
    "name" : "Jemmy overy",
    "data" : {...},
    "link" : "http:...",
}
Lix
  • 47,311
  • 12
  • 103
  • 131
Kivylius
  • 6,357
  • 11
  • 44
  • 71
  • 3
    If you struggle at understanding how to use JSON just look at the quite good [example page](http://json.org/example.html)! – Paul Mar 18 '13 at 22:11

2 Answers2

17

Btw, The answer by Lix below is best if you're looking to pull them out via their index number.

Wrap them in square brackets!

[{ 
    "id" : 5,
    "name" : "Jemmy overy",
    "data" : {...},
    "link" : "http:...",
},
{ 
    "id" : 6,
    "name" : "John Smith",
    "data" : {...},
    "link" : "http:...",
}]
castis
  • 8,154
  • 4
  • 41
  • 63
  • This just places the objects into an array. – Lix Mar 18 '13 at 22:11
  • I sorta took a leap and figured thats what he was getting at. – castis Mar 18 '13 at 22:13
  • @castis without the brakets ([]), it makes sence, just two different objects, id can also be remobved right, now how do i select the first one just obj[0], obj[1] ? – Kivylius Mar 18 '13 at 22:13
  • 1
    `obj[0]` will give you id 5 and `obj[1]` will give you 6 (In this case). The index used in the array has no correlation to the items ID property. – Lix Mar 18 '13 at 22:21
  • Thank you, i was not aware I could do this. – Kivylius Mar 18 '13 at 22:26
5

Well, the only way I can see to identify the JSON objects in that manner would be to use it's ID property as a key -

var a = {
  '5':{ 
    "id" : 5,
    "name" : "AAA",
    "data" : {...},
    "link" : "http:..."
  },'6':{ 
    "id" : 6,
    "name" : "BBB",
    "data" : {...},
    "link" : "http:..."
  },
  ...
};

So you access them like an array -

a['5']

With regard to sorting, I don't think that there is any native way to sort JSON objects, but there are some other posts on the site that provide a helping hand - Sorting JSON by values

Community
  • 1
  • 1
Lix
  • 47,311
  • 12
  • 103
  • 131