简体   繁体   中英

Nesting JSON Arrays and Objects

I have been using XML for a while now and have been reading about JSON being lighter and faster, so i am playing around with it a bit and trying to get a hang of it! the only problem is i have no idea as to how much of the syntax I've been using is correct.. if any one has any pointers for me it'd be really great! below is my attempt at nesting arrays and objects in json and this is my attempt at getting hold of that data also. Thanks, eggmaster

{
'page' : [{
    'article' : [{
        'block' : [{
            'title' : 'Title1-1',
            'instruction' : 'simon says',
            'body' : 'lorem dipsem ikhsduifohsdihfsjkahfksdlfklasdfh-0===-=-sklasdhjkfgaklf'
        }],
        'block' : [{
            'title' : 'Title1-2',
            'instruction' : 'simon stop says',
            'body' : 'lorem dipsem ikhsduifohsdihfsj58779kahfksdlfklasdfhsklasdhjkfgaklf'
        }]
    }],
    'article' : [{
        'block' : [{
            'title' : 'Title2-1',
            'instruction' : 'simon gp[g[says',
            'body' : 'lorem dipsem ikhsduifohsdihfsjkahfksdl56u456fklasdfhsklasdhjkfgaklf'
        }],
        'block' : [{
            'title' : 'Title2-2',
            'instruction' : 'sihehamon stop says',
            'body' : 'lorem dipsem ikhsduifohsdihfsjkahfksdlfkla-0-90-sdfhsklasdhjkfgaklf'
        }]
    }]
}],
'page' : [{
    'article' : [{
        'block' : [{
            'title' : 'Title2-1-1',
            'instruction' : 'simon says',
            'body' : 'lorem dipsem ikhsduifohsdihfsjkahfksdlfklasdfh-0===-=-sklasdhjkfgaklf'
        }],
        'block' : [{
            'title' : 'Title2-1-2',
            'instruction' : 'simon stop says',
            'body' : 'lorem dipsem ikhsduifohsdihfsj58779kahfksdlfklasdfhsklasdhjkfgaklf'
        }]
    }],
    'article' : [{
        'block' : [{
            'title' : 'Title2-2-1',
            'instruction' : 'simon gp[g[says',
            'body' : 'lorem dipsem ikhsduifohsdihfsjkahfksdl56u456fklasdfhsklasdhjkfgaklf'
        }],
        'block' : [{
            'title' : 'Title2-2-2',
            'instruction' : 'sihehamon stop says',
            'body' : 'lorem dipsem ikhsduifohsdihfsjkahfksdlfkla-0-90-sdfhsklasdhjkfgaklf'
        }]
    }]
}]
}

And the jquery to extract it..

$(document).ready(function(){

    $.getJSON('data.json', function(json){  
        alert(json.page[0].article[1].block[0].title)
    })

})

Use double-quotes instead of single quotes. Single quotes might work with eval() or jQuery, but they're not standard.

Also, in JSON, each object's keys must be unique, so your article object cannot have two block entries, for example. You could rewrite your data like this:

{
"pages": [{
    "articles": [{
        "blocks": [{
            "title": ...
        }, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM