我已经开发了nodejs api,它接受xml作为输入,我能够在nodejs中访问它并将xml转换为下面提到的json。
var data = {
"ns0:service1":{
"$":{
"xmlns:ns0":"http://www.google.com"
},
"ns0:messageheader":{
"$":{
"version":"1.0",
"xmlns:ns1":"http://www.google.com/logo"
},
"ns1:sourcesystemcode":"MUST",
"ns1:operation":"Process",
"ns1:targetsystemlist":{
"ns1:targetsystemcode":"TEST1",
"ns1:targetsystemname":"TEST1"
}
},
"ns0:messagedata":{
"ns3:messagedata":{
"$":{
"xmlns:ns3":"http://www.google.com/logo2"
},
"ns3:somessagerequestdata":{
"ns3:sorequestorderheader":{
"ns3:sourcecode":"TEST1",
"ns3:msgdate":"2014-05-28T11:48:31",
"ns3:deliveryaddress":{
"ns3:name":"John",
"ns3:streetname":"Latin",
"ns3:housenumber":"53"
},
"ns3:customeraddress":"",
"ns3:sorequestline":{
"ns3:orderid":"ord_001",
"ns3:linetype":"testing",
"ns3:itemnumber":"001",
"ns3:itemdescription":"iphonex",
"ns3:quantity":"1",
}
}
}
}
}
}
}
如何在节点js中访问“ ord_001”,“测试”,“ 001”,“ iphonex”,“ 1”等值。
预先感谢:)
答案 0 :(得分:0)
一种方法是使用括号符号:
data["ns0:service1"]["ns0:messagedata"]["ns3:messagedata"]["ns3:somessagerequestdata"]["ns3:sorequestorderheader"]["ns3:sorequestline"]["ns3:orderid"] will get you to ord_001.
data["ns0:service1"]["ns0:messagedata"]["ns3:messagedata"]["ns3:somessagerequestdata"]["ns3:sorequestorderheader"]["ns3:sorequestline"] will get you to the whole object you're looking for.
这些对象的设置方式相当粗糙。某种原因阻止了JSON.parse(data)正常工作。