1

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

})
3
  • Also, excuse the gibberish i was just populating it for test Commented Oct 30, 2012 at 10:15
  • Have tried to use a JSLint tool? It tells you first if your JSON is valid. here is an example jslint.com Commented Oct 30, 2012 at 10:25
  • thanks dude, thats well handy Commented Oct 30, 2012 at 10:33

1 Answer 1

1

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": ...
        }, ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.