0

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?

chrb33
  • 3
  • 1
  • 2
    I would look at Jackson XML Mapper [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization), The way I would go about it use Jackson to convert the Json to a POJO and then use the XML mapper to convert to XML – Ben Davies Feb 16 '22 at 10:01
  • Thanks, not quite what I was looking for but it can work with it. – chrb33 Feb 17 '22 at 08:03

1 Answers1

0

Based on this solution https://stackoverflow.com/a/19978281/9474700 first import library in your gradle

implementation 'org.json:json:20211205'

then do as below

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);

Second option

Use from underscore-java

import library

implementation 'com.github.javadev:underscore:1.74'

then

String xml = U.jsonToXml(jsonString);
behrad
  • 1,228
  • 14
  • 21
  • Thank you, I had already worked with the Json.Org and also with the underscore, both of which were not satisfactory for what I was looking for. I've now decided on the ObjectMapper and XmlMapper. – chrb33 Feb 17 '22 at 08:10