JSON - 如何正确解析它?

时间:2014-08-19 08:35:33

标签: javascript jquery json

我遇到问题以正确的格式解析JSON数据。这是我的JSON数据。我只是想从数据中抓取beneAcctNumber

{
"restFilteredInterRegisteredAccounts": {
        "101010-BPD Bali-Permata ALTO": {
            "beneficiaryNote": null,
            "beneficiaryName": "Permata ALTO",
            "accountCurrency": "IDR",
            "paymentType": null,
            "beneficiaryIdType": null,
            "transferLimit": null,
            "beneficiaryResident": null,
            "accountLimit": 5.0E7,
            "bankName": "BPD Bali",
            "lastModTime": null,
            "ibInterAcctRef": null,
            "bankCode": "000129",
            "beneficiaryEmail": null,
            "ibInterAcctStrcol1": null,
            "ibInterAcctStrcol2": null,
            "userId": 1551,
            "beneAcctNumber": "101010",
            "beneficiaryId": null,
            "mobileNumber": null
        }
    },
}

检索后我得到了未定义的值。我试用这个。

restFilteredInterRegisteredAccounts[0].beneAcctNumber

请建议。

4 个答案:

答案 0 :(得分:1)

restFilteredInterRegisteredAccounts不是数组,所以[0]未定义... 像这样的东西会这样做(假设jsonObject持有json数据):

jsonObject [“restFilteredInterRegisteredAccounts”] [“101010-BPD Bali-Permata ALTO”] [“beneAcctNumber”]

答案 1 :(得分:0)

restFilteredInterRegisteredAccounts包含对象,这不是数组,因此不能使用索引来获取值。 “101010-BPD Bali-Permata ALTO”表现为属性restFilteredInterRegisteredAccounts。我不确定“101010-BPD Bali-Permata ALTO”是否为有效的物业名称。理想情况下,它之间不应包含空格。如果这样有效,那么试试

  

restFilteredInterRegisteredAccounts [“101010-BPD Bali-Permata ALTO”]。beneAcctNumber

答案 2 :(得分:0)

var da = {
"restFilteredInterRegisteredAccounts": {
        "101010-BPD Bali-Permata ALTO": {
            "beneficiaryNote": null,
            "beneficiaryName": "Permata ALTO",
            "accountCurrency": "IDR",
            "paymentType": null,
            "beneficiaryIdType": null,
            "transferLimit": null,
            "beneficiaryResident": null,
            "accountLimit": 5.0E7,
            "bankName": "BPD Bali",
            "lastModTime": null,
            "ibInterAcctRef": null,
            "bankCode": "000129",
            "beneficiaryEmail": null,
            "ibInterAcctStrcol1": null,
            "ibInterAcctStrcol2": null,
            "userId": 1551,
            "beneAcctNumber": "101010",
            "beneficiaryId": null,
            "mobileNumber": null
        }
}, "somethingelse" : "whatever"
};

要访问媒体资源(此处为beneAcctNumber),我们将使用

da.restFilteredInterRegisteredAccounts['101010-BPD Bali-Permata ALTO'].beneAcctNumber

Working Demo

答案 3 :(得分:-2)

我已经找到了正确的答案。 100%工作。

var output = "";
$.each(restFilteredInterRegisteredAccounts, function(index, item) {
    output += item.beneAcctNumber ;
});