Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a nested multi dimensional json object

Tags:

json

jquery

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":true,
    "town":"Dublin",
    "state":"Ohio",
    "country":"USA",
    "products":2,
    "0":{
        "pic_id":"1500",
        "description":"Picture of a computer",
        "localion":"img.cloudimages.us/2012/06/02/computer.jpg",
        "type":"jpg",
        "childrenimages":2

        "0":{
        "pic_id":"15011",
        "description":"Picture of a cpu",
        "localion":"img.cloudimages.us/2012/06/02/mycpu.png",
        "type":"png"
          }
        "1":{
        "pic_id":"15012",
        "description":"Picture of a cpu two",
        "localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
        "type":"png"
          }
    },
    "1":{
        "pic_id":"1501",
        "description":"Picture of a cpu",
        "localion":"img.cloudimages.us/2012/06/02/cpu.png",
        "type":"png"
    }
};

Is this right or is there a convention i should follow if my object becomes too deeply nested.

like image 300
Gandalf Avatar asked Jun 05 '12 15:06

Gandalf


People also ask

How do I add a nested object to JSON?

To create a nested object, call createNestedObject() . To create a nested array, call createNestedArray() .

Can JSON objects be nested?

Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.

Can JSON have multiple objects?

You can pass a single JSON object to create a single element, or a JSON array of group JSON objects to create multiple elements.

How do you write an array of objects in JSON?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.


1 Answers

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 :)

like image 53
Dmitry Kudryavtsev Avatar answered Oct 06 '22 06:10

Dmitry Kudryavtsev