[英]Stringifying a Java Object throwing Error in JSON.parse Javascript
I have defined a class Email having following details: 我定义了一个具有以下详细信息的类电子邮件:
Email
: Email
:
String name;
String subject;
List<String> attachment;
String jsonContent;
....
In above class, jsonContent
variable is loaded with a strinigified json object. 在上面的类中,
jsonContent
变量加载了经过分层处理的json对象。
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. 我需要在客户端中解析电子邮件对象并在UI中呈现它。
But it throws parsing error for Email object in client, ie 但是它会为客户端中的电子邮件对象引发解析错误,即
JSON.parse(emailString);
because jsonContent
field is having double quotes within it. 因为
jsonContent
字段内有双引号。
It is a problem of stringifying a JAVA object having a jsonContent
variable which is already stringified. 这是对具有已字符串化的
jsonContent
变量的JAVA对象进行字符串化的问题。
One way to fix it is define jsonContent
variable as a object rather than as a String. 修复它的一种方法是将
jsonContent
变量定义为对象而不是String。 Is there any other fix for it? 还有其他解决方法吗?
Example Email JSON: 电子邮件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. 要将json对象存储在json对象中,您需要对其进行转义。 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) 现在,如果要在Java中进行编译,这就是您将其手动输入为Java字符串(执行代码段)时的样子。
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 这就是发送的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\"]}}"
}
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作为字符串,因为您可以将其作为对象传递(去掉它周围的引号,这样您就可以
"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);
如果您需要它作为javascript中的字符串,则可以执行
JSON.stringify(json.jsonContent);
to get the same result easier. 为了更容易获得相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.