I am converting raw XML input to JSON and I am having issues with the desired structure of the output (lists not displaying correctly). The XML structure being imported can differ to the example below so using a POJO and simply annotating is not feasible. Sample XML input below:
我正在将原始XML输入转换为JSON,并且我遇到了所需的输出结构问题(列表未正确显示)。导入的XML结构可能与下面的示例不同,因此使用POJO并简单地进行注释是不可行的。以下示例XML输入:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<gang>
<id>435dfb3f-1129-4375-b0f9-09955d7434cc</id>
<name>Brew's Crews</name>
<members>
<member>
<member>d3433b1c-a93d-4af1-b698-89fcd921e48d</member>
<dateJoined/>
</member>
<member>
<member>8ac9f5bc-5710-4cb1-a75d-839e211f0286</member>
<dateJoined/>
</member>
</members>
<anthem/>
<logo>http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1</logo>
</gang>
I am using Jackson to convert the XML to JSON (the XML is already a string and represented by the 'result' variable)
我使用Jackson将XML转换为JSON(XML已经是一个字符串,由'result'变量表示)
XmlMapper xmlMapper = new XmlMapper();
Object entry = xmlMapper.readValue(result, Object.class);
ObjectMapper jsonMapper = new ObjectMapper();
return jsonMapper.writeValueAsString(entry);
The following JSON is produced:
生成以下JSON:
{
"id": "435dfb3f-1129-4375-b0f9-09955d7434cc",
"name": "Brew's Crews",
"members": {
"member": {
"member": "8ac9f5bc-5710-4cb1-a75d-839e211f0286",
"dateJoined": null
}
},
"anthem": null,
"logo": "http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1"
}
My issue is that the 'members' elements should be grouped in a JSON array like the following:
我的问题是'members'元素应该分组在一个JSON数组中,如下所示:
"members":[{"member":blah,"dateJoined":null},{"member":blah2,"dateJoined":null}]
But they are not....is there a specific configuration needed to be done to the XML mapper or Object mapper to achieve these desired results?
但它们不是......是否需要对XML映射器或对象映射器进行特定配置才能实现这些预期结果?
1 个解决方案
#1
1
Jackson in general will not be able to process generalized types such as Object
s. When you do -
杰克逊一般不能处理像对象这样的通用类型。当你这样做 -
Object entry = xmlMapper.readValue(result, Object.class);
Jackson doesn't know the structure of the object it's supposed to deserialize.
杰克逊不知道它应该反序列化的对象的结构。
You can try this -
你可以尝试这个 -
class Member {
private String memberId;
private String dateJoined;
}
class Gang {
private String id;
private String name;
private List<Member> members;
private String anthem;
private String logo;
}
class Test {
public static void main(String[] args) {
String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Gang><id>435dfb3f-1129-4375-b0f9-09955d7434cc</id><name>Brew's Crews</name><members><member><memberId>d3433b1c-a93d-4af1-b698-89fcd921e48d</memberId><dateJoined/></member><member><memberId>8ac9f5bc-5710-4cb1-a75d-839e211f0286</memberId><dateJoined/></member></members><anthem/><logo>http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1</logo></Gang>";
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
Object entry = xmlMapper.readValue(result, Gang.class);
ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
System.out.println(jsonMapper.writeValueAsString(entry));
}
}
#1
1
Jackson in general will not be able to process generalized types such as Object
s. When you do -
杰克逊一般不能处理像对象这样的通用类型。当你这样做 -
Object entry = xmlMapper.readValue(result, Object.class);
Jackson doesn't know the structure of the object it's supposed to deserialize.
杰克逊不知道它应该反序列化的对象的结构。
You can try this -
你可以尝试这个 -
class Member {
private String memberId;
private String dateJoined;
}
class Gang {
private String id;
private String name;
private List<Member> members;
private String anthem;
private String logo;
}
class Test {
public static void main(String[] args) {
String result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Gang><id>435dfb3f-1129-4375-b0f9-09955d7434cc</id><name>Brew's Crews</name><members><member><memberId>d3433b1c-a93d-4af1-b698-89fcd921e48d</memberId><dateJoined/></member><member><memberId>8ac9f5bc-5710-4cb1-a75d-839e211f0286</memberId><dateJoined/></member></members><anthem/><logo>http://localhost:8080/cloud/master-index-record/raw/58338b91-2390-44a7-ac31-581c5dd921e1</logo></Gang>";
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
Object entry = xmlMapper.readValue(result, Gang.class);
ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
System.out.println(jsonMapper.writeValueAsString(entry));
}
}