First i hope the topic is not old, but i didn't find something. So either you can write me a solution hint or where i find the topic. Thanks. And here is my problem i get a json response string back. The String looks like this:
{
"total": 3,
"hasMore": true,
"tasks": [
{
"taskId": "1",
"processName": "request new requests",
},
{
"taskId": "2",
"processName": "request new requests",
},
{
"taskId": "3",
"processName": "request new requests",
}
],
"totalSize": 6,
"nextIndex": 5,
"userTypeInTask": false
}
My goal is to convert this string to XML as follows:
<tasks>
<task>
<taskId>1</taskId>
<processName>request new requests</processName>
</task>
<task>
<taskId>2</taskId>
<processName>request new requests</processName>
</task>
<task>
<taskId>2</taskId>
<processName>request new requests</processName>
</task>
<totalSize>6</totalSize>
<total>3</total>
<hasMore>true</hasMore>
<nextIndex>5</nextIndex>
<userTypeInTask>false</userTypeInTask>
</tasks>
I managed to get the array but then I lose the other information.
import org.json.JSONArray;
import org.json.JSONObject;
public class jsontest {
@SuppressWarnings("null")
public static void main(String[] args) {
String jsonstring = "{\"total\": 3,\"hasMore\": true,\"tasks\":[{\"taskId\":\"1\",\"processName\": \"request new requests\"},{\"taskId\":\"2\",\"processName\": \"request new requests\"},{\"taskId\":\"2\",\"processName\": \"request new requests\"}],\"totalSize\": 6,\"nextIndex\": 5,\"userTypeInTask\": false}";
String parent = "tasks";
String child = "task";
JSONObject json = new JSONObject(jsonstring);
System.out.println("json: " + json);
if (json.has(parent)) {
JSONObject dataObject = json.optJSONObject(parent);
if (dataObject != null) {
System.out.println("dataObject 1: " + dataObject);
} else {
System.out.println("dataObject 2: " + dataObject);
JSONArray array = dataObject.getJSONArray(parent);
System.out.println("array: " + array);
String xml = "<" + parent + ">" + org.json.XML.toString(array, child) + "</" + parent + ">";
System.out.println("xml: " + xml);
}
}
}
}
Does anyone have a suggestion for my problem?