如何浏览 JSON 节点结构
我目前正在开发一个 Chrome 扩展程序,它调用一个向我发送 SOAP XML 消息的 Web 服务。当我收到此消息时,我必须对其进行导航以解析它,但我遇到了一些问题。
xml的结构是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns1="/service.php?service=components"
xmlns:enc="http://www.w3.org/2003/05/soap-encoding"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">
<ns1:getComponentsResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<rpc:result>return</rpc:result>
<return enc:itemType="enc:Struct" enc:arraySize="7" xsi:type="enc:Array">
<item xsi:type="enc:Struct">
<m_type xsi:type="xsd:string">image</m_type>
<m_params enc:itemType="enc:Struct" enc:arraySize="4" xsi:type="enc:Array">
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">image</m_name>
<m_value xsi:type="xsd:string">9f00de47f9f07ec0c7389875e4739c2b.png</m_value>
<m_numType xsi:type="xsd:int">3</m_numType>
</item>
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">weblink</m_name>
<m_value xsi:type="xsd:string">http://www.xxx.com</m_value>
<m_numType xsi:type="xsd:int">2</m_numType>
</item>
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">redirect-type</m_name>
<m_value xsi:type="xsd:string">2</m_value>
<m_numType xsi:type="xsd:int">0</m_numType>
</item>
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">is-logo</m_name>
<m_value xsi:type="xsd:string">1</m_value>
<m_numType xsi:type="xsd:int">0</m_numType>
</item>
</m_params>
<m_order xsi:type="xsd:int">0</m_order>
<m_childs enc:itemType="xsd:anyType" enc:arraySize="0" xsi:type="enc:Array"/>
<m_id xsi:type="xsd:int">119</m_id>
<m_name xsi:type="xsd:string">image_4d624ce18aeb3</m_name>
</item>
代码是这样的:
var json = $.xmlToJSON(xmlhttp.responseXML);
alert(json.item[0].length);
所以基本上我需要浏览“item”,但是它不起作用,我不明白为什么......
I'm currently developing a Chrome extension, which call a webservice that send me a SOAP XML message. When i received this message, i have to navigate on it to parse it but i'm having some problem with it.
The structure of the xml is like this :
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns1="/service.php?service=components"
xmlns:enc="http://www.w3.org/2003/05/soap-encoding"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">
<ns1:getComponentsResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<rpc:result>return</rpc:result>
<return enc:itemType="enc:Struct" enc:arraySize="7" xsi:type="enc:Array">
<item xsi:type="enc:Struct">
<m_type xsi:type="xsd:string">image</m_type>
<m_params enc:itemType="enc:Struct" enc:arraySize="4" xsi:type="enc:Array">
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">image</m_name>
<m_value xsi:type="xsd:string">9f00de47f9f07ec0c7389875e4739c2b.png</m_value>
<m_numType xsi:type="xsd:int">3</m_numType>
</item>
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">weblink</m_name>
<m_value xsi:type="xsd:string">http://www.xxx.com</m_value>
<m_numType xsi:type="xsd:int">2</m_numType>
</item>
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">redirect-type</m_name>
<m_value xsi:type="xsd:string">2</m_value>
<m_numType xsi:type="xsd:int">0</m_numType>
</item>
<item xsi:type="enc:Struct">
<m_name xsi:type="xsd:string">is-logo</m_name>
<m_value xsi:type="xsd:string">1</m_value>
<m_numType xsi:type="xsd:int">0</m_numType>
</item>
</m_params>
<m_order xsi:type="xsd:int">0</m_order>
<m_childs enc:itemType="xsd:anyType" enc:arraySize="0" xsi:type="enc:Array"/>
<m_id xsi:type="xsd:int">119</m_id>
<m_name xsi:type="xsd:string">image_4d624ce18aeb3</m_name>
</item>
The code is like this :
var json = $.xmlToJSON(xmlhttp.responseXML);
alert(json.item[0].length);
So basicelly i need to navigate throught the "item", but it didn't works and i don't understand for which reason...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 XML 只有一个根标签,因此当 xmlToJSON 插件进行解析时,它似乎并没有将顶级
视为子级,而是将其视为根也包括物体本身。我会尝试:
...或者您感兴趣的任何子元素。
如果此实现是 http://www.terracoder.com/index.php/xml-objectifier/xml-objectifier-examples ,你可以这样做:
我会使用像 http://phpjs.org/functions/var_export:578 (或者自己使用 for.. .in) 来反思这些对象,这样您就可以亲自了解它的组件是什么。
更新
我对使用 jQuery 与 Chrome 扩展一无所知,所以我的测试是使用独立版本。您可以首先尝试验证您的 XML 是否适用,然后从那里开始。例如,如果您包含 http://www.terracoder.com/scripts/saXMLUtils 中的 XMLObjectifier,则此方法有效首先.js:
您也可以在 XML 上尝试此操作:
Since XML has only one root tag, when the xmlToJSON plug-in does the parsing, it seems it is not treating the top-level
<item/>
as a child, but rather as the root of the object itself as well.I'd try:
...or whatever child element you were interested in.
If this implementation is the one at http://www.terracoder.com/index.php/xml-objectifier/xml-objectifier-examples , you can do:
I'd use a utility like http://phpjs.org/functions/var_export:578 (or do it yourself by using for...in) to reflect on objects such as these, so you can see for yourself what its components are.
UPDATE
I don't know anything about using jQuery with Chrome extensions, so my tests were with the stand-alone version. You might try verifying your XML works with that first, and go from there. E.g., this works if you include the XMLObjectifier from http://www.terracoder.com/scripts/saXMLUtils.js first:
You might also try this on your XML: