【问题标题】:how to access data from json converted from xml nodejs api如何访问从 xml nodejs api 转换的 json 中的数据
【发布时间】:2019-03-16 21:14:40
【问题描述】:

我开发了接受xml作为输入的nodejs api,我能够在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”“testing”“001”“iphonex”“1”等值。

【问题讨论】:

    标签: node.js json xml api


    【解决方案1】:

    一种方法是使用括号表示法:

    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) 工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2017-12-08
      相关资源
      最近更新 更多