将 xml 存储在 json 对象中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16906010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:49:51  来源:igfitidea点击:

storing xml inside json object

xmljsonstoring-information

提问by chappalprasad

I need to store complete xml document as part of json object. when i receive the request and try to create json object from json string like below -

我需要将完整的 xml 文档存储为 json 对象的一部分。当我收到请求并尝试从 json 字符串创建 json 对象时,如下所示 -

{"content":{
"name" : "xyz",
"details":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
 <ns0:Report xmlns:ns0=\"http://www.khisko.com/triTypes\">
  <StackTrace>Job-8004 Error in [xxxxxxxxxx]
      Output data invalid&#xD;
  at com.xyz.tst.a(Unknown Source)&#xD;
      caused by: java.lang.NullPointerException&#xD;
   </StackTrace>
   <Msg>Output data invalid</Msg>
  </ns0:Report>"
 }}

I am getting Unterminated string error at first char of details. how can i handle it.

我在详细信息的第一个字符处收到未终止的字符串错误。我该如何处理。

i am using org.json.JSONObject constructor which takes java string as parameter and passing above json as java string.

我正在使用 org.json.JSONObject 构造函数,它以 java 字符串作为参数并在 json 之上作为 java 字符串传递。

Thanks

谢谢

回答by bitsabhi

You can encode and decode xml string like

您可以像这样编码和解码 xml 字符串

{
  "content": {
    "name": "xyz",
    "details": "PD94bWwgdmVyc2lvbj1cIjEuMFwiIGVuY29kaW5nPVwiVVRGLThcIj8+CiA8bnMwOlJlcG9ydCB4bWxuczpuczA9XCJodHRwOi8vd3d3LmtoaXNrby5jb20vdHJpVHlwZXNcIj4KICA8U3RhY2tUcmFjZT5Kb2ItODAwNCBFcnJvciBpbiBbeHh4eHh4eHh4eF0KICAgICAgT3V0cHV0IGRhdGEgaW52YWxpZCYjeEQ7CiAgYXQgY29tLnh5ei50c3QuYShVbmtub3duIFNvdXJjZSkmI3hEOwogICAgICBjYXVzZWQgYnk6IGphdmEubGFuZy5OdWxsUG9pbnRlckV4Y2VwdGlvbiYjeEQ7CiAgIDwvU3RhY2tUcmFjZT4KICAgPE1zZz5PdXRwdXQgZGF0YSBpbnZhbGlkPC9Nc2c+CiAgPC9uczA6UmVwb3J0Pg==",
    "encoding": "base64"
  }
}

回答by AITAALI_ABDERRAHMANE

Just I've changed \"to 'and remove line breakslike @Explosion Pills says

只是我已经改变\"'并且remove line breaks喜欢@Explosion Pills 说

{"content":{
    "name" : "xyz",
    "details":"<?xml version='1.0' encoding='UTF-8'?>
     <ns0:Report xmlns:ns0='http://www.khisko.com/triTypes'>
      <StackTrace>Job-8004 Error in [xxxxxxxxxx]
          Output data invalid&#xD;
      at com.xyz.tst.a(Unknown Source)&#xD;
          caused by: java.lang.NullPointerException&#xD;
       </StackTrace>
       <Msg>Output data invalid</Msg>
      </ns0:Report>"
     }}

回答by chappalprasad

Don't want to remove the line breaks from the xml but made change in json java class to not throw unterminated string exception for NL, CR. thanks Explosion Pills.

不想从 xml 中删除换行符,但在 json java 类中进行了更改,以免为 NL、CR 抛出未终止的字符串异常。感谢爆炸丸。