我有这样的 JSON:
{
"cod":"200",
"message":0.0085,
"cnt":37,
"list":[
{
"dt":1492074000,
"main":{
"temp":8.74,
"temp_min":8.39,
"temp_max":8.74,
"pressure":1002.4,
"sea_level":1023.01,
"grnd_level":1002.4,
"humidity":94,
"temp_kf":0.35
},
"weather":[
{
"id":500,
"main":"Rain",
"description":"Lekki deszcz",
"icon":"10d"
}
],
"clouds":{
"all":32
},
(...)
JSON.parse(json.list[0]['weather'][0].description);
//OR
JSON.parse(json.list[0]['weather'][0]['description']);
我一无所获。但是如果我想访问这个数组 "id":500 中的 int,一切都很好。
JSON.parse(json.list[0]['weather'][0].id);
因此在这个特定示例中解析字符串看起来像是一个问题。
最佳答案
JSON.parse 只是不会为 JSON.parse(500)
抛出错误,因为您可以在数字上使用它。您已经有一个 json 对象(否则您的行 JSON.parse(json.list[0]['weather'][0].id);
也会失败)。
JSON.parse(500) 的输出是 500:
console.log(JSON.parse(500))
但是,当你尝试 JSON.parse 非 json 时,你会得到一个错误:
console.log(JSON.parse("foo"))
也就是说,您根本不需要 JSON.parse,因为它仅用于解析 JSON。要得到你想要的,只需使用
console.log(json.list[0].weather[0].description);
关于javascript - 解析 JSON - 数组的 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43417873/