JSON到XML的转换而不改变序列

5kgi1eie  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(111)

我使用下面的代码将多个具有不同JSON结构的XML文件的json转换为xml。

String toXmlRequest = fullRequest.toString();               
JSONObject jsonObj = new JSONObject(toXmlRequest);              
String XmlRequest = XML.toString(jsonObj);
System.out.println(XmlRequest);

输入

{
      "EnrollmentRequest":
        {
          "data": 
            {
              "commonDataContext":
                   {
                      "requestId": "ADA12131",
                      "clientId": "ABCDEF",
                      "timestamp":"2013-12-13T11:10:00.715-05:00"
                   },
               "cardNumber" : "123456789012345" ,
               "firstName" : "John" ,
               "lastName" : "Smith" ,
               "email" : "JohnSmith@g.com" ,
               "enrollStatus" : "E" , 
               "pathEnroll" : "NewAcct",
               "cardSavedIndicator" : "Y"
          }
        }
     }

产出

<EnrollmentRequest>
          <data>
              <firstName>John</firstName>
              <lastName>Smith</lastName>
              <commonDataContext>
                    <clientId>ABCDEF</clientId>
                    <requestId>ADA12131</requestId>
                    <timestamp>2013-12-13T11:10:00.715-05:00</timestamp>
              </commonDataContext>
              <pathEnroll>NewAcct</pathEnroll>
              <enrollStatus>E</enrollStatus>
              <cardSavedIndicator>Y</cardSavedIndicator>
              <cardNumber>123456789012345</cardNumber>
              <email>JohnSmith@g.com</email>
          </data>
      </EnrollmentRequest>

输出的顺序正在改变。它不能保持实际的顺序。有没有什么办法可以保持完整。

pzfprimi

pzfprimi1#

直接使用org.json.JSONObject是不可能的,原因是JSONObject使用HashMap类型的内部存储。HashMap不保留插入顺序
使用LinkedHashMap是可能的,但是似乎不可能将JSONObject配置为使用LinkedHashMap

/**
 * Construct an empty JSONObject.
 */
public JSONObject() {
    this.map = new HashMap<String, Object>();
}

另一种选择是使用确实保持顺序的库来读取,例如Jackson...。

ObjectMapper mapper = new ObjectMapper();
JsonNode jackson = mapper.readTree(fullRequest);

然后将其输入XML

String xmlRequest = XML.toString(new JSONAdapter(jackson));

进行必要的类型调整,使Jackson对象看起来像org.json.JSONObject。下面是不完整的示例:

private static class JSONAdapter extends JSONObject {

    private JsonNode jackson;

    public JSONAdapter(JsonNode jackson) {
        this.jackson = jackson;
    }

    @Override
    public Iterator<String> keys() {
        return jackson.fieldNames();
    }

    @Override
    public Object opt(String key) {
        return get(key);
    }

    @Override
    public Object get(String key) throws JSONException {
        JsonNode nested = jackson.get(key);
        if (nested.isObject()) {
            return new JSONAdapter(nested);
        } else if (nested.isTextual()) {
            return nested.asText();
        } else if (nested.isNumber()) {
            return nested.asDouble();
        } else if (nested.isBoolean()) {
            return nested.asBoolean();
        }
        return null;
    }

}

产出

<EnrollmentRequest>
    <data>
        <commonDataContext>
            <requestId>ADA12131</requestId>
            <clientId>ABCDEF</clientId>
            <timestamp>2013-12-13T11:10:00.715-05:00</timestamp>
        </commonDataContext>
        <cardNumber>123456789012345</cardNumber>
        <firstName>John</firstName>
        <lastName>Smith</lastName>
        <email>JohnSmith@g.com</email>
        <enrollStatus>E</enrollStatus>
        <pathEnroll>NewAcct</pathEnroll>
        <cardSavedIndicator>Y</cardSavedIndicator>
    </data>
</EnrollmentRequest>
wbgh16ku

wbgh16ku2#

对于嵌入式JSON数组,您需要再应用一个条件来检查它是否为数组,并将其作为JSON数组返回。

if(nested.isArray()) {
            JSONArray arr = new JSONArray();
            for(JsonNode value : nested){
                arr.put(value.asText());
            }
            return arr;
        }

相关问题