I am studying json and i was wondering if this is the right way to write a multi dimensional json object that is nested.I wrote:
var foo = {
\"logged_in\":tr
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 :)
Don't write JSON. Seriously, except for simple configuration files, don't write JSON.
You have utilities to convert objects to a JSON string in most languages (if not any).
PHP: json_encode($array);
Javascript: JSON.stringify( obj );
Etc.
Writing JSON manually often leads to syntax errors. The kind that gives you headaches until you see that missing comma or w/e. You have good tools to do this, use them. You could compare to XML, but JSON has no tool (IDEs, text editor) saying "This syntax is wrong" while you're typing it. For example, no editor will tell you that you used a single quote instead of a double.
Just don't write JSON.
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"
}
]
};
Here is the proper format for your data (note i changed some data itself)
{
"logged_in":true,
"town":"Dublin",
"state":"Ohio",
"country":"USA",
"products":2,
"productinfo":[
{
"0":{
"pic_id":"1500",
"description":"Picture of a computer",
"localion":"img.cloudimages.us/2012/06/02/computer.jpg",
"type":"jpg",
"childrenimages":2
},
"1":{
"pic_id":"15011",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/mycpu.png",
"type":"png"
},
"2":{
"pic_id":"15012",
"description":"Picture of a cpu two",
"localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
"type":"png"
},
"3":{
"pic_id":"1501",
"description":"Picture of a cpu",
"localion":"img.cloudimages.us/2012/06/02/cpu.png",
"type":"png"
}
}
]
}