Consider using arrays instead of numerated object.
Arrays in json are defined using [] http://www.json.org/
Here is an example:
var foo = {
"logged_in":true,
"town":"Dublin",
"state":"Ohio",
"country":"USA",
"products":
[
{
"pic_id":"1500",
"description":"Picture of a computer",
"localion":"img.cloudimages.us/2012/06/02/computer.jpg",
"type":"jpg",
"childrenimages":
[
{
"pic_id":"15011",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/mycpu.png",
"type":"png"
},
{
"pic_id":"15012",
"description":"Picture of a cpu two",
"localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
"type":"png"
}
]
},
{
"pic_id":"1501",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/cpu.png",
"type":"png"
}
],
};
(Forgive me if I forgot either closing { or [ or , its pretty hard to type code in SO :p )
This way you dont even need to have counts like
"products":2,
or
"childrenimages":2
You simply do
foo.products.length
or
foo.products[0].childrenimages.length
Good luck :)
Answer from Dmitry Kudryavtsev on Stack OverflowConsider using arrays instead of numerated object.
Arrays in json are defined using [] http://www.json.org/
Here is an example:
var foo = {
"logged_in":true,
"town":"Dublin",
"state":"Ohio",
"country":"USA",
"products":
[
{
"pic_id":"1500",
"description":"Picture of a computer",
"localion":"img.cloudimages.us/2012/06/02/computer.jpg",
"type":"jpg",
"childrenimages":
[
{
"pic_id":"15011",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/mycpu.png",
"type":"png"
},
{
"pic_id":"15012",
"description":"Picture of a cpu two",
"localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
"type":"png"
}
]
},
{
"pic_id":"1501",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/cpu.png",
"type":"png"
}
],
};
(Forgive me if I forgot either closing { or [ or , its pretty hard to type code in SO :p )
This way you dont even need to have counts like
"products":2,
or
"childrenimages":2
You simply do
foo.products.length
or
foo.products[0].childrenimages.length
Good luck :)
That's not actually an array that you have there, that's just an object containing properties that are also objects. You're also missing a few commas so this won't even compile.
What might be more convenient for you in this case is to use both arrays and objects to form your JSON. For Example:
var this_json_string = {
"state":"Ohio",
"country":"USA",
"products":[
{
"pic_id":"1500",
"description":"Picture of a computer",
},
{
"pic_id":"15011",
"description":"Picture of a cpu"
},
{
"pic_id":"15012",
"description":"Picture of a cpu two"
},
{
"pic_id":"1501",
"description":"Picture of a cpu"
}
]
};
Videos
an object literal has no .length
you can count properties using this method:
var count = 0;
for (i in jsonString) {
if (jsonString.hasOwnProperty(i)) {
count++;
}
}
alert(count); //count shall have length for you
OR
since your array didn't have numeric indices (starting from 0), it assumed you used an associative array, hence they dumped an object of items rather than an array of items.
to turn them to numeric indices, all you have to do is use array_values before encoding them to json:
json_encode(array_values($array));
then the json will be an array.. then you can use length
from this:
Array(
[1] => Array(
[product_id] => 1
[product_model] => HFJ5G1.5
[product_type] => plat
[product_return] => graviteits
)
[2] => Array(
[product_id] => 2
[product_model] => HHJ5S2.5
[product_type] => holle plunjer
[product_return] => veer
)
);
it becomes this using array_values(), note the indexes per item:
Array(
[0] => Array(
[product_id] => 1
[product_model] => HFJ5G1.5
[product_type] => plat
[product_return] => graviteits
)
[1] => Array(
[product_id] => 2
[product_model] => HHJ5S2.5
[product_type] => holle plunjer
[product_return] => veer
)
);
then encoded to json and stored to jsonString:
jsonString = [
{
"product_id": "1",
"product_model": "HFJ5G1.5",
"product_type": "plat",
"product_return": "graviteits"
},
{
"product_id": "2",
"product_model": "HHJ5S2.5",
"product_type": "holle plunjer",
"product_return": "veer"
}
];
alert(jsonString.length);
Objects do not support length property: alert({}.length); gives undefined and alert([].length); gives 0. To find the 'length' of the top level you could do it like:
var arr ={"1":{"product_id":"1","product_model":"HFJ5G1.5",
"product_type":"plat","product_return":"graviteits"},
"2":{"product_id":"2","product_model":"HHJ5S2.5",
"product_type":"holle plunjer","product_return":"veer"}};
var len = 0;
for(var i in arr) len++;
alert(len);
http://jsfiddle.net/uszaV/
Hey sorry if this is not the right sub, I am having an issue with this multidimensional json array that I want to turn into an excel table but whenever I do so, it only shows the first two dimensions which I really don't care about and it would be such a hassle to go through the file and delete every dimension and sign that I don't care about.
here is what the file looks like and I am only interested in the transit size:
{"step": {"p2p": {}, "collective": {"hcom_allReduce__852_0_1@5136442222858339852": {"Communication Time Info": {"Start Timestamp(us)": 1722279519921441.0, "Elapse Time(ms)": 12.6548, "Transit Time(ms)": 4.160679999999999, "Wait Time(ms)": 0.4504600000000002, "Synchronization Time(ms)": 2e-05, "Idle Time(ms)": 8.043660000000001, "Wait Time Ratio": 0.0977, "Synchronization Time Ratio": 0.0}, "Communication Bandwidth Info": {"RDMA": {"Transit Size(MB)": 0, "Transit Time(ms)": 0, "Bandwidth(GB/s)": 0, "Large Packet Ratio": 0, "Size Distribution": {}}, "HCCS": {"Transit Size(MB)": 469.7620479999989, "Transit Time(ms)": 29.189260000000015, "Bandwidth(GB/s)": 16.0937, "Large Packet Ratio": 0.0, "Size Distribution": {"2.097152": [224, 29.189260000000015]}}, "PCIE": {"Transit Size(MB)": 0, "Transit Time(ms)": 0, "Bandwidth(GB/s)": 0, "Large Packet Ratio": 0, "Size Distribution": {}}, "SDMA": {"Transit Size(MB)": 469.7620479999989, "Transit Time(ms)": 29.189260000000015, "Bandwidth(GB/s)": 16.0937, "Large Packet Ratio": 0, "Size Distribution": {}}, "SIO": {"Transit Size(MB)": 0, "Transit Time(ms)": 0, "Bandwidth(GB/s)": 0, "Large Packet Ratio": 0, "Size Distribution": {}}}}}}}