简体   繁体   English

将转义的json转换为xml

[英]convert json which escaped quotes to xml

i have the following json-string: 我有以下json-string:

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

which i want to convert to xml. 我想将其转换为xml。 I have tried several methods to convert, one of which is decribed here: Java implementation of JSON to XML conversion : 我尝试了几种方法进行转换,其中一种在这里介绍: JSON到Java的Java实现XML转换

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 我知道如果json字符串是

{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? 在java中?

Well escaping the quotes means they are escaped and the string is no longer json. 好的转义引号意味着它们已被转义并且字符串不再是json。 What you are trying to do is not within any kind of json standard - so you have to fix the problem yourself ;-) 您要尝试执行的操作不在任何json标准之内-因此您必须自己解决问题;-)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM