Im trying to convert JsonObject to XML but it seems to be encored.
我试图将JsonObject转换为XML,但它似乎被加入了。
This is my JsonObject -
这是我的JsonObject -
{
"customerName": "cus1",
"invoiceNumber": "in1",
"invoiceDate": "2017-01-23",
"amount": 110.1,
"invoiceItems": [
{
"chargeName": "null",
"subscriptionName": "TestSub",
"amount": 129,
"servicePeriod": "2017-01-23to 2017-02-23"
},
{
"subscriptionName": "",
"servicePeriod": "",
"chargeDate": "",
"chargeName": "Discounted Amount",
"amount": -12.9
}
]
}
Output I'm getting is -
我得到的输出是 -
{"customerName":"cus1;,"invoiceNumber":"in1;,"invoiceDate":"2017-01-23","amount":116.1,"invoiceItems":[{"chargeName":"null","subscriptionName":"TestSubd","amount":129.0,"servicePeriod":"2017-01-23to 2017-02-23"},{"subscriptionName":"","servicePeriod":"","chargeDate":"","chargeName":"Discounted Amount","amount":-12.9}]}"
Output im expecting is without encoding <customerName>cus1<customerName>
format.
输出im期望没有编码
I have used org.json.XML
to convert the json object to xml
我使用org.json.XML将json对象转换为xml
JsonObject invoiceDetailObj = new JsonObject();
invoiceDetailObj.addProperty("customerName", aa.get("customer").asText());
I added properties to the invoiceDetailObj so that its currently looks like the jsonObj I have added in the top
我在invoiceDetailObj中添加了属性,以便它当前看起来像我在顶部添加的jsonObj
xml = XML.toString(invoiceDetailObj);
1 个解决方案
#1
1
Your problem is related to the fact that you mix com.google.gson.JsonObject
from Google Gson with org.json.JSONObject
. Actually, the method XML.toString(object)
expects an instance of org.json.JSONObject
or org.json.JSONArray
or an array of org.json.JSONObject
so what you get is simply the default behavior of this method when none of those types are found.
您的问题与将Google Gson中的com.google.gson.JsonObject与org.json.JSONObject混合的事实有关。实际上,方法XML.toString(object)需要一个org.json.JSONObject或org.json.JSONArray的实例或一个org.json.JSONObject数组,所以你得到的只是这个方法的默认行为,当没有这些时找到类型。
Simply rewrite your code to use org.json.JSONObject
instead of com.google.gson.JsonObject
, your code should then look like something like this:
只需重写代码以使用org.json.JSONObject而不是com.google.gson.JsonObject,您的代码应如下所示:
JSONObject invoiceDetailObj = new JSONObject();
invoiceDetailObj.put("customerName", "cus1");
invoiceDetailObj.put("invoiceNumber", "in1");
...
String xml = XML.toString(invoiceDetailObj);
Or even better, if you have your JSON
object as a String
, you could simply use the constructor JSONObject(String source)
to let it parse and build the JSONObject
for you:
或者甚至更好,如果你把你的JSON对象作为一个String,你可以简单地使用构造函数JSONObject(String source)让它为你解析和构建JSONObject:
String xml = XML.toString(new JSONObject(myJSONString));
#1
1
Your problem is related to the fact that you mix com.google.gson.JsonObject
from Google Gson with org.json.JSONObject
. Actually, the method XML.toString(object)
expects an instance of org.json.JSONObject
or org.json.JSONArray
or an array of org.json.JSONObject
so what you get is simply the default behavior of this method when none of those types are found.
您的问题与将Google Gson中的com.google.gson.JsonObject与org.json.JSONObject混合的事实有关。实际上,方法XML.toString(object)需要一个org.json.JSONObject或org.json.JSONArray的实例或一个org.json.JSONObject数组,所以你得到的只是这个方法的默认行为,当没有这些时找到类型。
Simply rewrite your code to use org.json.JSONObject
instead of com.google.gson.JsonObject
, your code should then look like something like this:
只需重写代码以使用org.json.JSONObject而不是com.google.gson.JsonObject,您的代码应如下所示:
JSONObject invoiceDetailObj = new JSONObject();
invoiceDetailObj.put("customerName", "cus1");
invoiceDetailObj.put("invoiceNumber", "in1");
...
String xml = XML.toString(invoiceDetailObj);
Or even better, if you have your JSON
object as a String
, you could simply use the constructor JSONObject(String source)
to let it parse and build the JSONObject
for you:
或者甚至更好,如果你把你的JSON对象作为一个String,你可以简单地使用构造函数JSONObject(String source)让它为你解析和构建JSONObject:
String xml = XML.toString(new JSONObject(myJSONString));