ruhul080
ruhul080

Reputation: 45

json data retrieve by jquery

How can I get the id of info1 or info2 with each information of the inside loop by jquery loop. Fo example I want to get id 1 from info1 then all the information within id 1 similarly same as from info2. I need the output to show in the browser.

   var data = {
    "info1": {
        "1": {
            "clientname": "ruhul yahoo",
            "clientemail": "[email protected]",
            "clientusername": "ruhulya"
        },
        "2": {
            "clientname": "kaosar yahoo",
            "clientemail": "[email protected]",
            "clientusername": "kaosar"
        }
    },
    "info2": {
        "3": {
            "id": "24",
            "receiver": "5",
            "content": "chat system",
            "time": "2015-08-19 12:09:19"
        },
        "4": {
            "id": "23",
            "receiver": "4",
            "content": "chat system",
            "time": "2015-08-19 12:09:19"
        }
    },

}

Thanks in advance.

Upvotes: 0

Views: 59

Answers (4)

stubiklaus
stubiklaus

Reputation: 151

You can iterate over this object like this

for(var i in data){
    for(var j in data[i]){
        console.log(data[i][j]);
    }
}

Upvotes: 0

Jijo John
Jijo John

Reputation: 1375

Iterate the json array and access the object like the following code.

 var data = {
    "info1": {
        "1": {
            "clientname": "ruhul yahoo",
            "clientemail": "[email protected]",
            "clientusername": "ruhulya"
        },
        "2": {
            "clientname": "kaosar yahoo",
            "clientemail": "[email protected]",
            "clientusername": "kaosar"
        }
    },
    "info2": {
        "3": {
            "id": "24",
            "receiver": "5",
            "content": "chat system",
            "time": "2015-08-19 12:09:19"
        },
        "4": {
            "id": "23",
            "receiver": "4",
            "content": "chat system",
            "time": "2015-08-19 12:09:19"
        }
    },

};


for(var j in data){
   for(var k in data[j]){
      console.log(data[j][k]); 
  }
}

Your browser's Console will log the following objects if you run the above example.

    Object {clientname: "ruhul yahoo", clientemail: "[email protected]", clientusername: "ruhulya"}
    Object {clientname: "kaosar yahoo", clientemail: "[email protected]", clientusername: "kaosar"}
    Object {id: "24", receiver: "5", content: "chat system", time: "2015-08-19 12:09:19"}
   Object {id: "23", receiver: "4", content: "chat system", time: "2015-08-19 12:09:19"}

Then you can access the values like a normal object console.log(data[j][k].clientname);

Upvotes: 1

Tushar Gupta
Tushar Gupta

Reputation: 15933

Firstly, This is not a valid JSON, Rmove the last , before last {

Secondly , parse it as a JSON and get the info as

data.info1[1].clientname

var data = JSON.parse('{"info1":{"1":{"clientname":"ruhul yahoo","clientemail":"[email protected]","clientusername":"ruhulya"},"2":{"clientname":"kaosar yahoo","clientemail":"[email protected]","clientusername":"kaosar"}},"info2":{"3":{"id":"24","receiver":"5","content":"chat system","time":"2015-08-19 12:09:19"},"4":{"id":"23","receiver":"4","content":"chat system","time":"2015-08-19 12:09:19"}}}');
alert(data.info1[1].clientname);
alert(data.info1[2].clientname);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Upvotes: 0

Zac Braddy
Zac Braddy

Reputation: 611

This function will find you the first instance of a variable name in the object. If you need to find a variable in a specific path you could amend this function fairly easily to do that. Certainly the function as is passes the test case you've provided.

function findVar(data, varName) {    
    for (var i in data) {
        if (i === varName) return data[i];

        if (typeof (data[i]) === 'object') {
            var findResult = findVar(data[i], varName)
            if (typeof(findResult) !== 'undefined') 
            {
                return findResult;
            }
        }
    }

    return undefined;
}

Upvotes: 0

Related Questions