我正在使用xml序列化程序将我的JSON输出转换为XML。但我在xml输出中获取元素。如何管理这个。我输入的json字符串如下所示。
String responseBodyStr = "{\"spimObject\":[{\"client\":\"test1\",\"createDate\":1399641291687,\"createdBy\":\"dan\",\"endDate\":1407535200000,\"phase\":\"Doing\",\"projDesc\":\"Description\",\"projName\":\"Project 1832\",\"startDate\":1394319600000,\"_id\":\"536cd4cbe4b0265e5978fbcd\",\"owner\":\"dan\"},{\"client\":\"HP\",\"createDate\":1399641326758,\"createdBy\":\"dan\",\"endDate\":1407535200000,\"phase\":\"Doing\",\"projDesc\":\"Description\",\"projName\":\"Project 1832\",\"startDate\":1394319600000,\"_id\":\"536cd4eee4b0265e5978fbce\",\"owner\":\"dan\"},{\"client\":\"Swiss\",\"createDate\":1399911549732,\"createdBy\":\"dan\",\"endDate\":1402524000000,\"phase\":\"Doing\",\"projDesc\":\"Hotel for Swiss of 500 bedrooms\",\"projName\":\"Swiss\",\"startDate\":1399845600000,\"_id\":\"5370f47de4b0265e5978fbd9\",\"owner\":\"dan\"},{\"client\":\"Best Western\",\"createDate\":1400245867385,\"createdBy\":\"dan\",\"endDate\":1402869600000,\"phase\":\"Doing\",\"projDesc\":\"Description\",\"projName\":\"test2\",\"startDate\":1400191200000,\"_id\":\"53760e6be4b0265e5978fbda\",\"owner\":\"dan\"},{\"client\":\"Best Western\",\"createDate\":1400589659884,\"createdBy\":\"dan\",\"endDate\":1403215200000,\"phase\":\"Doing\",\"projDesc\":\"Description\",\"projName\":\"hotel\",\"startDate\":1400536800000,\"_id\":\"537b4d5be4b0265e5978fbdb\",\"owner\":\"dan\"}]}";
输出正如: -
<?xml version="1.0" encoding="UTF-8"?>
<o><spimObject><e><_id>536cd4cbe4b0265e5978fbcd</_id><client>test1</client><createDate>1399641291687</createDate><createdBy>dan</createdBy><endDate>1407535200000</endDate><owner>dan</owner><phase>Doing</phase><projDesc>Description</projDesc><projName>Project 1832</projName><startDate>1394319600000</startDate></e><e><_id>536cd4eee4b0265e5978fbce</_id><client>HP</client><createDate>1399641326758</createDate><createdBy>dan</createdBy><endDate>1407535200000</endDate><owner>dan</owner><phase>Doing</phase><projDesc>Description</projDesc><projName>Project 1832</projName><startDate>1394319600000</startDate></e><e><_id>5370f47de4b0265e5978fbd9</_id><client>Swiss</client><createDate>1399911549732</createDate><createdBy>dan</createdBy><endDate>1402524000000</endDate><owner>dan</owner><phase>Doing</phase><projDesc>Hotel for Swiss of 500 bedrooms</projDesc><projName>Swiss</projName><startDate>1399845600000</startDate></e><e><_id>53760e6be4b0265e5978fbda</_id><client>Best Western</client><createDate>1400245867385</createDate><createdBy>dan</createdBy><endDate>1402869600000</endDate><owner>dan</owner><phase>Doing</phase><projDesc>Description</projDesc><projName>test2</projName><startDate>1400191200000</startDate></e><e><_id>537b4d5be4b0265e5978fbdb</_id><client>Best Western</client><createDate>1400589659884</createDate><createdBy>dan</createdBy><endDate>1403215200000</endDate><owner>dan</owner><phase>Doing</phase><projDesc>Description</projDesc><projName>hotel</projName><startDate>1400536800000</startDate></e></spimObject></o>
我的java代码逻辑: -
if(JSONSerializer.toJSON(responseBodyStr) instanceof JSONArray){
jsonArray = (JSONArray)JSONSerializer.toJSON(responseBodyStr);
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setRemoveNamespacePrefixFromElements(true);
xmlSerializer.setSkipNamespaces(true);
xmlSerializer.clearNamespaces();
xmlSerializer.setTypeHintsEnabled(false);
jsonXmlString = xmlSerializer.write(jsonArray);
}else if (JSONSerializer.toJSON(responseBodyStr) instanceof JSONObject) {
jsonObject = (JSONObject) JSONSerializer.toJSON(responseBodyStr);
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(false);
xmlSerializer.setRemoveNamespacePrefixFromElements(true);
xmlSerializer.setSkipNamespaces(true);
xmlSerializer.clearNamespaces();
jsonXmlString = xmlSerializer.write(jsonObject);
}
System.out.println("jsonXmlString is:\n" + jsonXmlString);
return jsonXmlString;
添加了Json输入: -
{ “spimObject”:[{ “客户端”: “测试1”, “CREATEDATE”:1399641291687 “createdBy”: “丹”, “结束日期”:1407535200000, “相”: “做”, “projDesc”: “Description”,“projName”:“Project 1832”,“startDate”:1394319600000,“_ id”:“536cd4cbe4b0265e5978fbcd”,“owner”:“dan”}, { “客户端”: “HP”, “CREATEDATE”:1399641326758, “createdBy”: “丹”, “结束日期”:1407535200000, “相”: “做”, “projDesc”: “说明”, “projName”:” Project 1832“,”startDate“:1394319600000,”_ id“:”536cd4eee4b0265e5978fbce“,”owner“:”dan“}, {“client”:“Swiss”,“createDate”:1399911549732,“createdBy”:“dan”,“endDate”:1402524000000,“phase”:“doing”,“projDesc”:“500间卧室的瑞士酒店”, “projName”: “瑞士”, “的startDate”:1399845600000, “_ ID”: “5370f47de4b0265e5978fbd9”, “主人”: “丹”}, {“client”:“Best Western”,“createDate”:1400245867385,“createdBy”:“dan”,“endDate”:1402869600000,“phase”:“Doing”,“projDesc”:“Description”,“projName”: “TEST2”, “的startDate”:1400191200000, “_ ID”: “53760e6be4b0265e5978fbda”, “主人”: “丹”}, {“client”:“Best Western”,“createDate”:1400589659884,“createdBy”:“dan”,“endDate”:1403215200000,“phase”:“Doing”,“projDesc”:“Description”,“projName”: “旅馆”, “的startDate”:1400536800000, “_ ID”: “537b4d5be4b0265e5978fbdb”, “所有者”: “丹”}]}