Jackson: XML to Map with List deserialization

前端 未结 3 680
暖寄归人
暖寄归人 2020-12-15 05:52

Is there a way to deserialize the following xml into Map holding List of items using Jackson?


    12345678
    <         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 06:43

    The other answers don't work if you have to use readTree() and JsonNode. I know it's an ugly solution but at least you don't need to paste someone's gist in your project.

    Add org.json to your project dependencies.

    And then do the following:

    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.json.JSONObject;
    import org.json.XML;
    ...
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    ...
        JSONObject soapDatainJsonObject = XML.toJSONObject(data);
        return OBJECT_MAPPER.readTree(soapDatainJsonObject.toString());
    

    The conversion goes as follows:

    XML -> JSONObject (using org.json) -> string -> JsonNode (using readTree)

    Of course, toJSONObject handles duplicates without any problems, I suggest to avoid using Jackson and readTree() if you can.

提交回复
热议问题