jquery - How to write a nested multi dimensional json object -


i studying json , wondering if right way write multi dimensional json object nested.i wrote:

var foo = {     "logged_in":true,     "town":"dublin",     "state":"ohio",     "country":"usa",     "products":2,     "0":{         "pic_id":"1500",         "description":"picture of computer",         "localion":"img.cloudimages.us/2012/06/02/computer.jpg",         "type":"jpg",         "childrenimages":2          "0":{         "pic_id":"15011",         "description":"picture of cpu",         "localion":"img.cloudimages.us/2012/06/02/mycpu.png",         "type":"png"           }         "1":{         "pic_id":"15012",         "description":"picture of cpu two",         "localion":"img.cloudimages.us/2012/06/02/thiscpu.png",         "type":"png"           }     },     "1":{         "pic_id":"1501",         "description":"picture of cpu",         "localion":"img.cloudimages.us/2012/06/02/cpu.png",         "type":"png"     } }; 

is right or there convention should follow if object becomes nested.

consider using arrays instead of numerated object.

arrays in json defined using [] http://www.json.org/

here example:

var foo = {     "logged_in":true,     "town":"dublin",     "state":"ohio",     "country":"usa",     "products":     [         {             "pic_id":"1500",             "description":"picture of computer",             "localion":"img.cloudimages.us/2012/06/02/computer.jpg",             "type":"jpg",             "childrenimages":             [                 {                     "pic_id":"15011",                     "description":"picture of cpu",                     "localion":"img.cloudimages.us/2012/06/02/mycpu.png",                     "type":"png"                 },                 {                     "pic_id":"15012",                     "description":"picture of cpu two",                     "localion":"img.cloudimages.us/2012/06/02/thiscpu.png",                     "type":"png"                 }             ]         },         {             "pic_id":"1501",             "description":"picture of cpu",             "localion":"img.cloudimages.us/2012/06/02/cpu.png",             "type":"png"         }     ], }; 

(forgive me if forgot either closing { or [ or , pretty hard type code in :p )

this way dont need have counts like

"products":2, 

or

"childrenimages":2 

you do

foo.products.length 

or

foo.products[0].childrenimages.length 

good luck :)


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -