XML到JSON的转换问题 [英] XML to JSON Conversion Issues

查看:154
本文介绍了XML到JSON的转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种参考架构将XML转换为JSON的方法.

Is there a way of converting XML to JSON with reference to a schema.

让我们说xsd的一部分如下:

Lets say a part of the xsd is as below:

  <xs:complexType name="RegionStateResultType">
    <xs:sequence>
      <xs:element name="RegionOrState"     type="xs:string"  minOccurs="0" maxOccurs="1"/>
      <xs:element name="RegionOrStateCode" type="xs:string"  minOccurs="0" maxOccurs="1"/>
      <xs:element name="PinCode" type="xs:integer"  minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

,而xml是:

<RegionOrStateInfo>
    <RegionOrState>Tochigi</RegionOrState>
    <RegionOrStateCode>09</RegionOrStateCode>
    <PinCode>12345</PinCode>
</RegionOrStateInfo>

在使用XMLtoJSON转换策略时,如果我们将RecognizeNumber设置为'true',则得到的json是:

On Using XMLtoJSON conversion Policy , if we set RecognizeNumber as 'true' then resulting json is:

{
    "RegionOrState": "Tochigi",
    "RegionOrStateCode": "09" ,
    "PinCode":12345
},
{
    "RegionOrState": "Tokushima",
    "RegionOrStateCode": 36 ,
    "PinCode":12345
}

如果RecognizeNumber设置为"false",则生成的json是:

If RecognizeNumber is set as 'false' then the resulting json is:

{
    "RegionOrState": "Tochigi",
    "RegionOrStateCode": "09" ,
    "PinCode": "12345"
},
{
    "RegionOrState": "Tokushima",
    "RegionOrStateCode": "36" ,
    "PinCode": "12345"
}

所需的json是:

{
    "RegionOrState": "Tochigi",
    "RegionOrStateCode": "09" ,
    "PinCode": 12345
},
{
    "RegionOrState": "Tokushima",
    "RegionOrStateCode": "36" ,
    "PinCode": 12345
}

实现这一目标的任何方法吗?

Any way of achieving this?

有人使用过JSONSchema吗?转换器是否有办法引用架构然后进行翻译?

Has anyone used JSONSchema and is there a way for the converter to refer to a schema and then do the translation?

我还遇到了XML到JSON转换中的单元素数组的一些问题.认为JSONSchema可以有所帮助

I also faced some issues with single element arrays in XML to JSON conversion. Thinking is JSONSchema can be of some help

推荐答案

当数组仅包含一个对象时,我会遇到相同的apigee数组处理问题. Apigee不维护xsd,因此当apigee包含单个对象时,很难理解它.我已添加了两个JavaScript政策来解决该问题.

I faced same issue of apigee array handling when array holds only one object. Apigee doesn't maintain xsd so it is difficult for apigee to understand an array when it contains single object. I have added to two javascript policies to resolve the issue.

策略编号1:在通过apigee将xml转换为json之前,在流中应用此策略.

Policy Number 1:This policy is applied in the flow before converting the xml to json by apigee.

目的:在每个数组中添加银行对象.

Purpose: Add bank object in each array.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="Modify-XML-response">
    <DisplayName>Modify XML response</DisplayName>
    <FaultRules/>
    <Properties/>
    <ResourceURL>jsc://modifyXMLResponse.js</ResourceURL>
</Javascript>

modifyXMLResponse.js

这里我要添加一个空白的 Sfiwfalertquerynotes 对象,其中该数组至少具有一个Sfiwfalertquerynotes对象.

modifyXMLResponse.js

Here I am adding a blank Sfiwfalertquerynotes object where the array has at least one Sfiwfalertquerynotes object.

var jsonResponse = response.content;
var returnStr = jsonResponse;
returnStr = returnStr.split("</Sfiwfalertquerynotes>").join("</Sfiwfalertquerynotes><Sfiwfalertquerynotes/>");
response.content = returnStr; 

策略编号2:在通过apigee将xml转换为json之后,该策略将在流中应用.

Policy Number 2:This policy is applied in the flow after converting the xml to json by apigee.

目的:因为当apigee将xml转换为json时,我们在xml数组中添加了一个空白对象,因此每个数组将具有{}或"之类的空白对象.我们需要清理每个阵列中的所有这些.

Purpose: Because we added a blank object in the xml arrays when apigee converted the xml to json, each array will have blank object like {} or "". We need to cleanup all these from each array.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="modifyjsonresponse">
    <DisplayName>Modify Json response</DisplayName>
    <FaultRules/>
    <Properties/>
    <ResourceURL>jsc://modifyJsonResponse.js</ResourceURL>
</Javascript>

modifyJsonResponse.js

var jsonResponse = response.content;
var returnStr = jsonResponse;
returnStr = returnStr.split("},\"\"").join("}")
response.content = returnStr; 

目标端点预流:

<PreFlow name="PreFlow">
    <Request>
        <Step>
            <Name>SetTargetURL</Name>
        </Step>
    </Request>
    <Response>
        <Step>
            <Name>Modify-XML-response</Name>
        </Step>
        <Step>
            <Name>XmltoJson</Name>
        </Step>
        <Step>
            <FaultRules/>
            <Condition>(content-type = "application/xml") or (content-type = "text/xml")</Condition>
            <Name>ResponseJsontoXml</Name>
        </Step>
        <Step>
            <Name>modifyjsonresponse</Name>
        </Step>
    </Response>
</PreFlow>

这篇关于XML到JSON的转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆