gpt4 book ai didi

Javascript,读取具有动态内容的 json 时出现问题

转载 作者:行者123 更新时间:2023-11-28 15:50:24 25 4
gpt4 key购买 nike

我有一个像这样的json:

[
{
"Code": 100
},
{
"Drivers": [
{
"RowID": "1",
"DriverName": "ZZ",
"DriverAlias": "FF",
"DriverEmail": "YY@gmail.com",
"DriverPhone": "1121112229"
},
{
"RowID": "3",
"DriverName": "OO",
"DriverAlias": "EE",
"DriverEmail": "kkk@gmail.com",
"DriverPhone": "12121458474"
}
]
}
]

我正在像这样读取 Javascript 中的 json:

for (var i = 0; i < data.length; i++) {  
Drivers[i] = {
RowID: data[1].Drivers[i].RowID,
DriverName: data[1].Drivers[i].DriverName,
DriverAlias: data[1].Drivers[i].DriverAlias,
DriverEmail: data[1].Drivers[i].DriverEmail,
DriverPhone: data[1].Drivers[i].DriverPhone
};
}

它正在工作,但是当我只有一个像这样的驱动程序的 json 时:

[
{
"Code": 100
},
{
"Drivers": [
{
"RowID": "1",
"DriverName": "ZZ",
"DriverAlias": "FF",
"DriverEmail": "YY@gmail.com",
"DriverPhone": "1121112229"
}
]
}
]

它不起作用,我是否以错误的方式读取 json?谢谢。

最佳答案

试试这个 - 您需要检查数据的长度[1]。驱动程序:

for (var i = 0; i < data[1].Drivers.length; i++) {  
Drivers[i] = {
RowID: data[1].Drivers[i].RowID,
DriverName: data[1].Drivers[i].DriverName,
DriverAlias: data[1].Drivers[i].DriverAlias,
DriverEmail: data[1].Drivers[i].DriverEmail,
DriverPhone: data[1].Drivers[i].DriverPhone
};
}

关于Javascript,读取具有动态内容的 json 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830435/

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