JSON到XML转换而不更改序列

时间:2017-03-14 13:27:52

标签: java json xml

我使用下面的代码将json转换为具有不同JSON结构的多个XML文件的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>

输出的顺序正在改变。它无法保持实际顺序。有什么方法可以保持完整。

1 个答案:

答案 0 :(得分:1)

直接使用org.json.JSONObject无法做到这一点。原因是JSONObject使用HashMap类型的内部存储。 HashMap不保留插入顺序

可以使用LinkedHashMap,但似乎无法配置JSONObject来使用它。

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

另一种方法是使用保留顺序的库来阅读,例如:杰克逊....

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>