我有以下json:
{
"armament": {
"misil_aire_aire": [
{
"noms": {
"nom_rus": "R-60",
"nom_otan": "AA-8 Aphid"
},
"pes": "43.5",
"cap_explosiu": "3",
"caracteristiques_dinamiques": {
"velocitat_mach": "2.7",
"abast": "8",
"sistema_de_guia": "infraroig"
},
"pag_web": "https://en.wikipedia.org/wiki/R-60_(missile)"
},
{
"noms": {
"nom_rus": "R-27",
"nom_otan": "AA-10 Alamo"
},
"pes": "253",
"cap_explosiu": "39",
"caracteristiques_dinamiques": {
"velocitat_mach": "4.5",
"abast": "80",
"sistema_de_guia": "radar"
},
"pag_web": "https://en.wikipedia.org/wiki/R-27_(air-to-air_missile)"
},
{
"noms": {
"nom_rus": "R-73",
"nom_otan": "AA-11 Archer"
},
"pes": "105",
"cap_explosiu": "7.4",
"caracteristiques_dinamiques": {
"velocitat_mach": "2.5",
"abast": "20",
"sistema_de_guia": "infraroig"
},
"pag_web": "https://en.wikipedia.org/wiki/R-73_(missile)"
},
{
"noms": {
"nom_rus": "R-77",
"nom_otan": "AA-12 Adder"
},
"pes": "175",
"cap_explosiu": "22.5",
"caracteristiques_dinamiques": {
"velocitat_mach": "4.5",
"abast": "80",
"sistema_de_guia": "radar"
},
"pag_web": "https://en.wikipedia.org/wiki/R-77"
},
{
"noms": {
"nom_rus": "R-3",
"nom_otan": "AA-2 Atoll"
},
"pes": "90",
"cap_explosiu": "7.4",
"caracteristiques_dinamiques": {
"velocitat_mach": "2.5",
"abast": "8",
"sistema_de_guia": "infraroig"
},
"pag_web": "https://en.wikipedia.org/wiki/K-13_(missile)"
},
{
"noms": {
"nom_rus": "R-23",
"nom_otan": "AA-7 Apex"
},
"pes": "222",
"cap_explosiu": "25",
"caracteristiques_dinamiques": {
"velocitat_mach": "3",
"abast": "35",
"sistema_de_guia": "radar"
},
"pag_web": "https://en.wikipedia.org/wiki/K-13_(missile)"
}
],
"misil_aire_superficie": [
{
"noms": {
"nom_rus": "Kh-25",
"nom_otan": "AS-10 Karen"
},
"pes": "299",
"cap_explosiu": "89.6",
"caracteristiques_dinamiques": {
"velocitat_mach": "1.58",
"abast": "11",
"sistema_de_guia": "laser"
},
"pag_web": "https://en.wikipedia.org/wiki/Kh-25"
},
{
"noms": {
"nom_rus": "Kh-29",
"nom_otan": "AS-14 Kedge"
},
"pes": "660",
"cap_explosiu": "320",
"caracteristiques_dinamiques": {
"velocitat_mach": "1.23",
"abast": "10",
"sistema_de_guia": "radar"
},
"pag_web": "https://en.wikipedia.org/wiki/Kh-29"
},
{
"noms": {
"nom_rus": "Kh-31",
"nom_otan": "AS-17 Krypton"
},
"pes": "610",
"cap_explosiu": "94",
"caracteristiques_dinamiques": {
"velocitat_mach": "2.7",
"abast": "25",
"sistema_de_guia": "guia inercial"
},
"pag_web": "https://en.wikipedia.org/wiki/Kh-31"
}
]
}
}
我想把一个
放在superficie_misils的所有nom_otan中,我正在努力做到以下几点:
function cargarLista(obj_Json){
avi = obj_Json.armamento.misil_aire_superficie;
for (var i = 0; i < avi.length; i++) {
misId = avi[i].noms.nom_otan;
document.getElementById("p1").innerHTML += mo + "<br>";
}
}
但它似乎不起作用,我该怎么办呢?我不知道如何到达那个节点。关于如何做的任何想法?
答案 0 :(得分:1)
对于初学者来说,这里有一个错字:
var avi = obj_Json.armamento.misil_aire_superficie;
//should be (make sure to use var so you don't populate your global scope)
var avi = obj_Json.armament.misil_aire_superficie;
其次,确保在传递给cargarLista()之前使用JSON.parse(yourJsonStringGoesHere)解析JSON对象,或者作为函数中的第一个动作。
var obj = JSON.parse(obj_Json);
答案 1 :(得分:0)
这实际上是由于拼写错误,
avi = obj_Json.armamento.misil_aire_superficie;
应该是
avi = obj_Json.armament.misil_aire_superficie;
答案 2 :(得分:0)
我认为你必须先解析Json ......你有json作为字符串,还是JSON?
如果Json是字符串,请尝试:
function cargarLista(strJson){
obj_json = JSON.parse(strJson);
avi = obj_Json.armamento.misil_aire_superficie;
for (var i = 0; i < avi.length; i++) {
misId = avi[i].noms.nom_otan;
document.getElementById("p1").innerHTML += mo + "<br>";
}
}
然后,您可以执行“var misil = obj_Json.armament.misil_aire_superficie;
”