gpt4 book ai didi

javascript - jquery 获取 json 数据

转载 作者:行者123 更新时间:2023-12-03 09:10:23 26 4
gpt4 key购买 nike

如何通过 jquery 循环获取内部循环的每个信息的 info1 或 info2 的 id。例如,我想从 info1 获取 id 1,然后 id 1 中的所有信息与从 info2 获取的信息类似。我需要在浏览器中显示输出。

   var data = {
"info1": {
"1": {
"clientname": "ruhul yahoo",
"clientemail": "ruhul080@yahoo.com",
"clientusername": "ruhulya"
},
"2": {
"clientname": "kaosar yahoo",
"clientemail": "kaosar080@yahoo.com",
"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"
}
},

}

提前致谢。

最佳答案

迭代 json 数组并访问对象,如以下代码所示。

 var data = {
"info1": {
"1": {
"clientname": "ruhul yahoo",
"clientemail": "ruhul080@yahoo.com",
"clientusername": "ruhulya"
},
"2": {
"clientname": "kaosar yahoo",
"clientemail": "kaosar080@yahoo.com",
"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]);
}
}

如果您运行上述示例,您的浏览器控制台将记录以下对象。

    Object {clientname: "ruhul yahoo", clientemail: "ruhul080@yahoo.com", clientusername: "ruhulya"}
Object {clientname: "kaosar yahoo", clientemail: "kaosar080@yahoo.com", 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"}

然后你就可以像普通对象一样访问这些值 console.log(data[j][k].clientname);

关于javascript - jquery 获取 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com