-2

i have this JSON Object, and i wanna access to this > object.foda.forta.id or name.. in JAVASCRIPT thanks

note: this json it's created by xml2js.Parser()

 {


 "object": {
    "foda": [
      {
        "forta": [
          {
            "id": [
              "1"
            ],
            "name": [
              "dasdghjg"
            ]
          },
          {
            "id": [
              "2"
            ],
            "name": [
              "jj"
            ]
          },
          {
            "id": [
              "3"
            ],
            "name": [
              "gjhjg"
            ]
          }
        ]
      }
    ]
  }
}
Mr. Newbie
  • 382
  • 5
  • 17
  • or maybe [Accessing nested JavaScript objects with string key](http://stackoverflow.com/q/6491463/218196), depending on what you really want to do. You haven't given a lot of information. – Felix Kling Apr 04 '14 at 04:54

2 Answers2

1

You cant access by object.foda.forta.id

as foda and fotra are lists, you can access by object.foda[0].forta[0].id

Note - 0 is used for sample only you can use any index (less the size of array)

onsy
  • 740
  • 4
  • 11
0

Your JSON was terribly created, lots of unnecessary Arrays, but you can access it like this:

var obj = {
    "object": {
        "foda": [
            {
                "forta": [
                    {
                        "id": ["1"],
                        "name": ["dasdghjg"]
                    },
                    {
                        "id": ["2"],
                        "name": ["jj"]
                    },
                    {
                        "id": ["3"],
                        "name": ["gjhjg"]
                    }
                ]
            }
        ]
    }
};

document.body.innerHTML = "ID: " + obj.object.foda[0].forta[0].id[0] + " - Name: " + obj.object.foda[0].forta[0].name[0];

Take a look at this fiddle!

Buzinas
  • 11,597
  • 2
  • 36
  • 58