0

i have the following json-string:

{result:{"id":"1234","pages": [{"Id":50,"data":"{\"name\":\"name1\",\"description\":\"description1\"}"}],"errors":[] }}

which i want to convert to xml. I have tried several methods to convert, one of which is decribed here: Java implementation of JSON to XML conversion :

JSONObject o = new JSONObject(jsonString);
String xml = org.json.XML.toString(o);

the result of this is:

<result><id>1234</id><pages><data>{&quot;name&quot;:&quot;name1&quot;,&quot;description&quot;:&quot;description1&quot;}</data><Id>50</Id></pages></result>

or formatted:

<result>
  <id>1234</id>
  <pages>
    <Id>50</Id>
    <data>{&quot;name&quot;:&quot;name1&quot;,&quot;description&quot;:&quot;description1&quot;}</data>
  </pages>
</result>

obviously the data in 'data' is converted to a string. Actually what I want is a structure like this, where the content of the 'data' block is converted to tags:

<result>
  <id>1234</id>
  <pages>
    <Id>50</Id>
    <data>
      <name>name1</name>
      <description>description1</description>
    </data>
  </pages>
</result>

i know this was the result if the json string would be

{result:{"id":"1234","pages": [{"Id":50,"data":{"name":"name1","description":"description1"}}],"errors":[]}}

but unfortunatelly i can not change that.

So does anyone know about a method to convert

{result:{"id":"1234","pages": [{"Id":50,"data":"{\"name\":\"name1\",\"description\":\"description1\"}"}],"errors":[] }}

to

<result>
  <id>1234</id>
  <pages>
    <Id>50</Id>
    <data>
      <name>name1</name>
      <description>description1</description>
    </data>
  </pages>
</result>

in java?

Community
  • 1
  • 1
tyler
  • 193
  • 1
  • 3
  • 11

1 Answers1

0

Well escaping the quotes means they are escaped and the string is no longer json. What you are trying to do is not within any kind of json standard - so you have to fix the problem yourself ;-)

How about this:

JSONObject o = new JSONObject(jsonString.replaceAll("\\\\\"", "\""));
String xml = org.json.XML.toString(o);

Update - there are many more problems - this will work:

JSONObject o = new JSONObject(
jsonString.replaceFirst("result", "\"result\"")
.replaceAll("\"\\{", "{")
.replaceAll("\\}\"", "}")
.replaceAll("\\\\\"", "\"") );
String xml = org.json.XML.toString(o);
Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
  • thank you for your answer, i have tried that before, but using `replaceAll("\\\"", "\"")` results in `1234{"name":"name1","description":"description1"}50{"name":"name2","description":"description2"}51` using `replaceAll("\\\\\"", "\"")` results in an exception org.json.JSONException: Expected a ',' or '}' at 51 [character 52 line 1] – tyler Mar 02 '16 at 10:10
  • My code sample was correct, but there are more problems: 1) the data object is surrounded by quotes that have to be removed after unescaping 2) the "result" key is not surrounded by quotes - they must be added. I have updated my code sample above with a fix for both. – Andreas Vogl Mar 02 '16 at 10:44
  • Btw. I have used a json validator to track down these additional isses. Might be helpful for you as well: https://jsonformatter.curiousconcept.com/ – Andreas Vogl Mar 02 '16 at 10:48
  • you are right, this works. i am not (yet) fully familiar with json syntax, so i did not spot these 'details'. thank you very much! – tyler Mar 02 '16 at 11:08