Cómo leer una matriz JSON con etiquetas @ # en datos usando jQuery,

{
    "locenter": [
        {
            "loname": {
                "@empid": "1001",
                "#text": "FE1"
            },
            "centers": [
                {
                    "@id": "0000100001",
                    "#text": "dcgiDal"
                }
            ]
        },
        {
            "loname": {
                "@empid": "1002",
                "#text": "FE2"
            },
            "centers": [
                {
                    "@id": "0000300006",
                    "#text": "dcgiDah"
                },
                {
                    "@id": "0000100006",
                    "#text": "dcgiDau"
                }
            ]
        }
    ]
}

preguntado el 09 de enero de 11 a las 06:01

are you having a specific problem with this? What have you tried and what makes @# so hard? -

got it locenter.centers["@id"] / locenter.centers["#text"] this is how i need to access -

Philip Potter i wanted to access @id and #text values !! got it thanks -

1 Respuestas

The same way you read any other JSON: jQuery.parseJSON or JSON.parse. Simplified:

var o = jQuery.parseJSON('{"loname": { "@empid": "1001", "#text": "FE1" } }');

Si utiliza jQuery.ajax es datatype 'json' (and possibly if you let it guess), it will do this for you.

After parsing it, you may have to use the array-like syntax to access properties. In Firefox at least, this is only necessary for the number sign:

alert(o.loname['#text']);

Demo

Respondido el 09 de enero de 11 a las 09:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.