我收到一个巨大的json对象,我只需要从中获取两个属性。
json就像这样
"{\r\n\"d\" : [\r\n{\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1191)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1191, \"PortName\": \"PORTLAND\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1192)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1192, \"PortName\": \"BANGOR\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1193)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1193, \"PortName\": \"LUBEC\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\":....
我只需要PortName和PortID就像这样
0:{portCode:"",portName:""},
1:{portCode:"",portName:""},
...
答案 0 :(得分:0)
var jsonString = "{\r\n\"d\" : [\r\n{\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1191)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1191, \"PortName\": \"PORTLAND\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1192)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1192, \"PortName\": \"BANGOR\"\r\n}, {\r\n\"__metadata\": {\r\n\"uri\": \"http:\/\/xx.xx.xx\/AciApi.svc\/Port_Domestic(1193)\", \"type\": \"ACIModel.Port_Domestic\"\r\n}, \"PortID\": 1193, \"PortName\": \"LUBEC\"\r\n}]}"
var jsonArray = JSON.parse(jsonString).d;
var arrayOfObjectsWithOnlyThose2Properties = jsonArray.map(function(item){
return {
"portCode": item.PortCode,
"portName": item.PortName
}
});
console.log(arrayOfObjectsWithOnlyThose2Properties);