0

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, i.e.

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 <[email protected]>",
    "to": [
        "undisclosed-recipients:;"
    ],
    "cc": [],
    "bcc": [
        "[email protected]"
    ],
    "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"
    ]
  }
}"
}
6
  • What is the error and what is the content of jsonContent in Java and in the browser? If you provide more detail, it will allow someone to help. Commented Apr 1, 2015 at 5:51
  • @SeanMickey I have added a sample Email JSON Commented Apr 1, 2015 at 6:12
  • @SeanMickey I am sorry for that, I have updated JSON, it will still give invalid JSON, but If you remove jsonContent part, it will give valid JSON Commented Apr 1, 2015 at 6:24
  • have you tried escaping double quotes with an escaped slash? \\\" Commented Apr 1, 2015 at 6:31
  • @MichaelDibbets Yes, I have tried that.. It did not work for me Commented Apr 1, 2015 at 6:38

1 Answer 1

1

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 <[email protected]>",
    "to": [
        "undisclosed-recipients:;"
    ],
    "cc": [],
    "bcc": [
        "[email protected]"
    ],
    "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 <[email protected]>",
    "to": [
        "undisclosed-recipients:;"
    ],
    "cc": [],
    "bcc": [
        "[email protected]"
    ],
    "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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.