Trouble making a JSON string -
i'm trying upgrade existing json structure more complex one.
the original idea bunch of x animals, each 1 having bunch of caracteristics:
- animals
- animal 1
- claws:4
- eyes:2
- animal 2
- claws:0
- eyes:6
- etc.
- animal 1
the json this:
{ "animals":[ {"claws":"4", "eyes":"2"}, {"claws":"0", "eyes":"6"}, etc. ] }
so can see, don't name each animal object, caracteristics of animal element of array. can use them in loop animals[x].claws
now want add nest each animal, like:
- animals
- animal 1
- head
- eyes:2
- ears:2
- body
- claws:4
- tails:1
- legs:4
- head
- animal 2
- head
- eyes:6
- ears:0
- body
- claws:0
- tails:0
- legs:8
- head
- animal 1
but didn't manage without naming each animal object (with same name "animal") , using arrays what's in animal:
{ "animals":[ {"animal":[ {"head": {"eyes":"2", "ears":"2"} }, {"body": {"claws":"4", "tails":"1", "legs":"4"} } ]}, {"animal":[ {"head": {"eyes":"6", "ears":"0"} }, {"body": {"claws":"0", "tails":"0", "legs":"8"} } ]} ] }
i find sucky because:
1) don't need name each animal structure (especially since named "animal") since wanna iterate on them array before;
2) don't need array head , body since know number , names of elements.
i wish use doesn't work:
{ "animals":[ { {"head": {"eyes":"2", "ears":"2"} }, {"body": {"claws":"4", "tails":"1", "legs":"4"} } }, { {"head": {"eyes":"6", "ears":"0"} }, {"body": {"claws":"0", "tails":"0", "legs":"8"} } } ] }
so there way of nesting without using arrays , names?
sure there is! hash key-value pair, , that's why need key, when use {}
. array simple collection. {}
can't simple collection (that's why had use keys, , latter 1 didn't work, because {}
in example simple collection, look: { {}, {}, {} ... }
, in case had [ {}, {}, {} ... ]
), can make simpler, don't nest many times:
{ "animals":[ { "head": { "eyes":"2", "ears":"2" }, "body": { "claws":"4", "tails":"1", "legs":"4" } }, # ... ] }
Comments
Post a Comment