user7128116
user7128116

Reputation:

Javascript JSon can't access tag

I have the following 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"
            }
        ]
    }
}

I want to put in a

all the nom_otan of the superficie_misils, i'm trying to do the following:

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>";


            }
        }

But it doesn't seem to work, how the hell can i do it? I'm not sure how to arrive to that node. Any idea on how to do it?

Upvotes: 0

Views: 49

Answers (3)

Federico Saenz
Federico Saenz

Reputation: 532

I think do you have to parse Json before... Do you have json as string, or as JSON?

If Json is string, try this:

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>";


            }
        }

Then, you can do "var misil = obj_Json.armament.misil_aire_superficie;"

Upvotes: 0

Per Henrik Jakobsson
Per Henrik Jakobsson

Reputation: 163

This is actually due to a typo,

    avi = obj_Json.armamento.misil_aire_superficie;

Should be

    avi = obj_Json.armament.misil_aire_superficie;

Upvotes: 0

hellojebus
hellojebus

Reputation: 3583

For starters, there is a typo here:

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;

Second, make sure you are parsing the JSON object using JSON.parse(yourJsonStringGoesHere), either before passing to cargarLista() or as the first action in your function.

var obj = JSON.parse(obj_Json);

Upvotes: 1

Related Questions