Search code examples
jsonnumberstraversal

How to traverse a number in JSON?


I've got this output from an API:

{
    "user": {
        "0": {
            "contact": "36",
            "user-id": "12233",
            "username": "user1",
            "date": 1370622179
        }
    }
}

I'd like to get the value of 'username', but the '0' is causing problems. I'm getting this error in chrome: Uncaught SyntaxError: Unexpected number

Here is the code I'm using to access it:

$.getJSON(dataAddr, function (data) {
    html += data.user.0.username;
    $("#upload").append("user: " + html);
});

Solution

  • Use [] notation. data.user["0"].username;