简体   繁体   中英

Stringifying a Java Object throwing Error in JSON.parse Javascript

I have defined a class Email having following details:
Email :

String name;
String subject;
List<String> attachment;
String jsonContent;   
....    

In above class, jsonContent variable is loaded with a strinigified json object.
Once an Email object is created, I am stringifying the whole Email object and sending to client.

I need to parse Email object in client and render it in UI.
But it throws parsing error for Email object in client, ie

JSON.parse(emailString);

because jsonContent field is having double quotes within it.
It is a problem of stringifying a JAVA object having a jsonContent variable which is already stringified.

One way to fix it is define jsonContent variable as a object rather than as a String. Is there any other fix for 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 the link to my recent presentation",
"jsonContent": "{
  "typeOfMail": "NormalMail",
  "normalMail": {
    "mailType": "NormalMail",
    "paragraphs": [
      "Pleasefindthelinktomyrecentpresentation"
    ]
  }
}"
}

You will need to escape a lot of strings to get stuff as strings.

to store a json object in a json object you need to escape it. so

  "jsonContent": "{
  "typeOfMail": "NormalMail",
  "normalMail": {
    "mailType": "NormalMail",
    "paragraphs": [
      "Pleasefindthelinktomyrecentpresentation"
    ]
  }
}"

becomes

"jsonContent": "{\\"typeOfMail\\": \\"NormalMail\\",\\"normalMail\\":{\\"mailType\\":\\"NormalMail\\",\\"paragraphs\\":[\\"Pleasefindthelinktomyrecentpresentation\\"]}}"

Now if you want to compile it in java, this is how it should look like if you would type it manually as an 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 the link to my recent presentation", "jsonContent": "{\\"typeOfMail\\": \\"NormalMail\\",\\"normalMail\\":{\\"mailType\\":\\"NormalMail\\",\\"paragraphs\\":[\\"Pleasefindthelinktomyrecentpresentation\\"]}}" } console.log("This is the json object having a string with json"); console.log(json); console.log("This is it parsed as 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 this is how it would look like in a 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 the link to my recent presentation",
"jsonContent": "{\"typeOfMail\": \"NormalMail\",\"normalMail\":{\"mailType\":\"NormalMail\",\"paragraphs\":[\"Pleasefindthelinktomyrecentpresentation\"]}}"
}

Now I don't see why you want jsonContent as a string, as you could just pass it as an object(remove the quotes surrounding it so you get

"jsonContent": {
  "typeOfMail": "NormalMail",
  "normalMail": {
    "mailType": "NormalMail",
    "paragraphs": [
      "Pleasefindthelinktomyrecentpresentation"
    ]
  }
}

and if you need it as string in javascript you can just do JSON.stringify(json.jsonContent); to get the same result easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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