简体   繁体   English

如何使用jQuery在数据中使用@#标签读取JSON数组,

[英]How to read JSON array with @ # tags in data using 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"
                }
            ]
        }
    ]
}

The same way you read any other JSON: jQuery.parseJSON or JSON.parse . 读取其他JSON的方式相同: jQuery.parseJSONJSON.parse Simplified: 简化:

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

If you use jQuery.ajax with datatype 'json' (and possibly if you let it guess), it will do this for you. 如果您将jQuery.ajaxdatatype 'json'一起使用(并且可能会猜中),它将为您完成此操作。

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: 至少在Firefox中,这仅对于数字符号是必需的:

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

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM