Reputation: 845
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:
{"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
at com.xyz.tst.a(Unknown Source)
caused by: java.lang.NullPointerException
</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.
Upvotes: 27
Views: 82691
Reputation: 845
I didn't want to remove the line breaks from the XML so I've changed the Java class that handles the JSON to not throw unterminated string exceptions for newlines or carriage returns.
Upvotes: 6
Reputation: 778
You can encode and decode xml string like
{
"content": {
"name": "xyz",
"details": "PD94bWwgdmVyc2lvbj1cIjEuMFwiIGVuY29kaW5nPVwiVVRGLThcIj8+CiA8bnMwOlJlcG9ydCB4bWxuczpuczA9XCJodHRwOi8vd3d3LmtoaXNrby5jb20vdHJpVHlwZXNcIj4KICA8U3RhY2tUcmFjZT5Kb2ItODAwNCBFcnJvciBpbiBbeHh4eHh4eHh4eF0KICAgICAgT3V0cHV0IGRhdGEgaW52YWxpZCYjeEQ7CiAgYXQgY29tLnh5ei50c3QuYShVbmtub3duIFNvdXJjZSkmI3hEOwogICAgICBjYXVzZWQgYnk6IGphdmEubGFuZy5OdWxsUG9pbnRlckV4Y2VwdGlvbiYjeEQ7CiAgIDwvU3RhY2tUcmFjZT4KICAgPE1zZz5PdXRwdXQgZGF0YSBpbnZhbGlkPC9Nc2c+CiAgPC9uczA6UmVwb3J0Pg==",
"encoding": "base64"
}
}
Upvotes: 32
Reputation: 2519
Just I've changed \"
to '
and remove line breaks
like @Explosion Pills says
{"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
at com.xyz.tst.a(Unknown Source)
caused by: java.lang.NullPointerException
</StackTrace>
<Msg>Output data invalid</Msg>
</ns0:Report>"
}}
Upvotes: 14