JSONobject组成HTMLtable

JSON object to form a HTML table

我需要从下面的 JSON 响应中提取值,我从网络服务响应中收到。

{"ns4:SearchSA_PartyReturn": {
    "xmlns:ns1": "urn:cs-base",
    "xsi:type": "ns4:SearchSA_PartyReturn",
    "ns4:object": {
        "xmlns:ns2": "urn:co-base",
        "recordCount": 3,
        "xmlns:ns0": "urn:cs-rest",
        "ns3:item": [
            {
                "ns3:SA_Party": {
                    "ns3:partyName": "Akorn New Jersey Inc",
                    "ns3:partyStatus": "ACTIVE",
                    "ns2:rowidObject": 20011,
                    "ns3:SAC_Address": {
                        "ns3:item": {
                            "ns3:city": "QUE",
                            "ns3:state": "N",
                            "ns2:rowidObject": 20011,
                            "ns3:postalCode": -4,
                            "ns3:addressType": "P",
                            "ns3:country": "US",
                            "ns3:addressLine": "RD"
                        },
                        "pageSize": 10,
                        "firstRecord": 1,
                        "searchToken": "SVR1.TD3V"
                    },
                    "ns3:partyType": "Pharma",
                    "ns3:SAC_Person": {
                        "ns3:item": {
                            "ns3:firstName": "DO",
                            "ns2:rowidObject": 11,
                            "ns3:lastName": "MA",
                            "ns3:middleName": "R",
                            "ns3:personType": 2
                        },
                        "pageSize": 10,
                        "firstRecord": 1,
                        "searchToken": "SVR1.TD3U"
                    }
                },
                "ns3:changeSummary": {
                    "logging": false,
                    "xmlns:sdo": "commonj.sdo"
                }
            },
            {
                "ns3:SA_Party": {
                    "ns3:partyName": "Akorn New Jersey Inc",
                    "ns3:partyStatus": "ACTIVE",
                    "ns2:rowidObject": 20047,
                    "ns3:SAC_Address": {
                        "ns3:item": {
                            "ns3:city": "SC",
                            "ns3:state": "N",
                            "ns2:rowidObject": 20047,
                            "ns3:postalCode": 12,
                            "ns3:addressType": "B",
                            "ns3:country": "US",
                            "ns3:addressLine": "OTT STET"
                        },
                        "pageSize": 10,
                        "firstRecord": 1,
                        "searchToken": "SVR1.TD3X"
                    },
                    "ns3:partyType": "Pharma",
                    "ns3:SAC_Person": {
                        "ns3:item": {
                            "ns3:firstName": "GE",
                            "ns2:rowidObject": 47,
                            "ns3:lastName": "HA",
                            "ns3:middleName": "B",
                            "ns3:personType": 2
                        },
                        "pageSize": 10,
                        "firstRecord": 1,
                        "searchToken": "SVR1.TD3W"
                    }
                },
                "ns3:changeSummary": {
                    "logging": false,
                    "xmlns:sdo": "commonj.sdo"
                }
            },
            {
                "ns3:SA_Party": {
                    "ns3:partyName": "Cig Na Ltd",
                    "ns3:partyStatus": "ACTIVE",
                    "ns2:rowidObject": 20040,
                    "ns3:SAC_Address": {
                        "ns3:item": {
                            "ns3:city": "JA",
                            "ns3:state": "NY",
                            "ns2:rowidObject": 20040,
                            "ns3:postalCode": 1,
                            "ns3:addressType": "P",
                            "ns3:country": "US",
                            "ns3:addressLine": "QUR"
                        },
                        "pageSize": 10,
                        "firstRecord": 1,
                        "searchToken": "SVR1.TD3Z"
                    },
                    "ns3:partyType": "Insurance",
                    "ns3:SAC_Person": {
                        "ns3:item": {
                            "ns3:firstName": "A",
                            "ns2:rowidObject": 40,
                            "ns3:lastName": "Q",
                            "ns3:personType": 2
                        },
                        "pageSize": 10,
                        "firstRecord": 1,
                        "searchToken": "SVR1.TD3Y"
                    }
                },
                "ns3:changeSummary": {
                    "logging": false,
                    "xmlns:sdo": "commonj.sdo"
                }
            }
        ],
        "pageSize": 10,
        "firstRecord": 1
    },
    "xmlns:ns3": "urn:co-ors",
    "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "xmlns:ns4": "urn:cs-ors"
}}

谁能帮我 javascript/jquery 提取 ns3:SA_Party 及其对应的 children ns3:SAC_Addressns3:SAC_Person。 (SA_Party 是 parent 和两个 children SAC_Address 和 SAC_Person)。我也需要底层属性。

假设 data 变量是包含您的响应的有效 JSON 对象

var items = data['ns4:SearchSA_PartyReturn']['ns4:object']['ns3:item'];

var mapped = items.map(function(item){ 
  return {
    'SAC_Address':item['ns3:SA_Party']['ns3:SAC_Address'], 
    'SAC_Person':item['ns3:SA_Party']['ns3:SAC_Person']
  }; 
});

console.log(mapped);

mapped 变量将是一个包含 SAC_AddressSAC_Person 信息的对象数组。

希望这对您有所帮助。

JSON.Parse() 方法将获取您的原始文本并将其转换为一个对象,前提是它的格式正确 JSON。您按如下方式传递 JSON 格式的字符串:

var jsonString = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var result = JSON.parse(jsonString);

那么就可以正常的通过点号来引用各个元素了:

var firstname = result.firstname;

但是由于您要查找的特定元素中有冒号,并且 Javascript 不支持其中包含冒号的标识符,因此您需要使用括号表示法,如