0
{
    "data": {
        "info": [
            {
                "username": "john",
                "uid": "893y4t89hg98y7894th",
                "avatar": null,
                "level": null
            },
            {
                "username": "joel",
                "uid": "ui9otjry4t89hg994t",
                "avatar": null,
                "level": null
            }
        ]
    }
}

This is the json data. Here is the jsfiddle.

JS:

$(function(){
    var str = '{"data":{"info":[{"username":"john","uid":"893y4t89hg98y7894th","avatar":null,"level":null},{"username":"joel","uid":"ui9otjry4t89hg994t","avatar":null,"level":null}]}}'; 
    str = JSON.parse(str.data.info);
    $.each(str, function(i, item){
        $(".result1").append(item.username);
        $(".result2").append(item.uid);
    });
});

Could someone tell me what have I done wrong?

4 Answers 4

3

A little wrong. You can not access the string as an object.

str = JSON.parse(str.data.info); //str is string, no object
$.each(str, function(i, item){
//... it wrong


str = JSON.parse(str);//parse string, get object
$.each(str.data.info, function(i, item){
//... it right

See DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$(function(){
    var str = '{"data":{"info":[{"username":"john","uid":"893y4t89hg98y7894th","avatar":null,"level":null},{"username":"joel","uid":"ui9otjry4t89hg994t","avatar":null,"level":null}]}}'; 
    str = JSON.parse(str);
    $.each(str.data.info, function(i, item){
        $(".result1").append(item.username);
        $(".result2").append(item.uid);
    });
});

Updated Fiddle

Comments

0

your parsing is wrong.You have ot make str a json object pefore getting values.

Try this:

$(function(){
var str = '{"data":{"info":[{"username":"john","uid":"893y4t89hg98y7894th","avatar":null,"level":null},{"username":"joel","uid":"ui9otjry4t89hg994t","avatar":null,"level":null}]}}'; 
str = JSON.parse(str);
var info=str['data']['info'];
console.log(info);
for(var key in info){
    $(".result1").append(info[key]['username']);
    $(".result2").append(info[key]['uid']);
}
});

Same updated in your fiddle.

7 Comments

No real need for the [] notation here.
Much harder to read in my opinion and also has a higher chance of typos with ['']
But if you want to access the keys at runtime, i think its the best way.
Sure. You are correct when you use [key] but not when you use ['data'] in my opinion
you can get it dear. Check the fiddle provided above. I updated it
|
0

This will work better - you need to parse the string before you can access an object in the JSON

$(function(){
    var str = '{"data":{"info":[{"username":"john","uid":"893y4t89hg98y7894th","avatar":null,"level":null},{"username":"joel","uid":"ui9otjry4t89hg994t","avatar":null,"level":null}]}}'; 
    str = JSON.parse(str);
    var info = str.data.info;
    $.each(info, function(i, item){
        $(".result1").append(item.username);
        $(".result2").append(item.uid);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="result1"></div>
<div class="result2"></div>

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.