Stringifying a Java Object throwing Error in JSON.parse Javascript -
i have defined class email having following details:
email
:
string name; string subject; list<string> attachment; string jsoncontent; ....
in above class, jsoncontent
variable loaded strinigified json object.
once email object created, stringifying whole email object , sending client.
i need parse email object in client , render in ui.
throws parsing error email object in client, i.e.
json.parse(emailstring);
because jsoncontent
field having double quotes within it.
problem of stringifying java object having jsoncontent
variable stringified.
one way fix define jsoncontent
variable object rather string. there other fix it?
example email json:
{ "id": "e4682ec0-a7c3-4f4d-abcd-f404f5fdb1eb", "entitytype": "email", "subject": "presentation 1", "from": "aaa <a@a.com>", "to": [ "undisclosed-recipients:;" ], "cc": [], "bcc": [ "jack.porter@forwardaccelerator.com" ], "recieveddate": 1423101398000, "recieveddatestring": "wed, 4 feb 2015 12:26:38 -0800", "bodytext": " please find link recent presentation", "jsoncontent": "{ "typeofmail": "normalmail", "normalmail": { "mailtype": "normalmail", "paragraphs": [ "pleasefindthelinktomyrecentpresentation" ] } }" }
you need escape lot of strings stuff strings.
to store json object in json object need escape it.
"jsoncontent": "{ "typeofmail": "normalmail", "normalmail": { "mailtype": "normalmail", "paragraphs": [ "pleasefindthelinktomyrecentpresentation" ] } }"
becomes
"jsoncontent": "{\"typeofmail\": \"normalmail\",\"normalmail\":{\"mailtype\":\"normalmail\",\"paragraphs\":[\"pleasefindthelinktomyrecentpresentation\"]}}"
now if want compile in java, how should if type manually java string(execute snippet)
var json = { "id": "e4682ec0-a7c3-4f4d-abcd-f404f5fdb1eb", "entitytype": "email", "subject": "presentation 1", "from": "aaa <a@a.com>", "to": [ "undisclosed-recipients:;" ], "cc": [], "bcc": [ "jack.porter@forwardaccelerator.com" ], "recieveddate": 1423101398000, "recieveddatestring": "wed, 4 feb 2015 12:26:38 -0800", "bodytext": " please find link recent presentation", "jsoncontent": "{\"typeofmail\": \"normalmail\",\"normalmail\":{\"mailtype\":\"normalmail\",\"paragraphs\":[\"pleasefindthelinktomyrecentpresentation\"]}}" } console.log("this json object having string json"); console.log(json); console.log("this parsed string"); var x = {hello:json.stringify(json)}; console.log(json.stringify(x).substring(10,json.stringify(x).length-2)); document.getelementbyid('content').textcontent = json.stringify(x).substring(10,json.stringify(x).length-2);
<div id="content"></div>
and how in json file/request answer thats sent
{ "id": "e4682ec0-a7c3-4f4d-abcd-f404f5fdb1eb", "entitytype": "email", "subject": "presentation 1", "from": "aaa <a@a.com>", "to": [ "undisclosed-recipients:;" ], "cc": [], "bcc": [ "jack.porter@forwardaccelerator.com" ], "recieveddate": 1423101398000, "recieveddatestring": "wed, 4 feb 2015 12:26:38 -0800", "bodytext": " please find link recent presentation", "jsoncontent": "{\"typeofmail\": \"normalmail\",\"normalmail\":{\"mailtype\":\"normalmail\",\"paragraphs\":[\"pleasefindthelinktomyrecentpresentation\"]}}" }
now don't see why want jsoncontent string, pass object(remove quotes surrounding get
"jsoncontent": { "typeofmail": "normalmail", "normalmail": { "mailtype": "normalmail", "paragraphs": [ "pleasefindthelinktomyrecentpresentation" ] } }
and if need string in javascript can json.stringify(json.jsoncontent);
same result easier.
Comments
Post a Comment