我已经定义了一个类电子邮件,其中包含以下详细信息:
Email
:
String name;
String subject;
List<String> attachment;
String jsonContent;
....
在上面的类中,jsonContent
变量加载了一个strinigified json对象
创建Email对象后,我将整个Email对象进行字符串化并发送给客户端。
我需要解析客户端中的Email对象并在UI中呈现它 但它会在客户端抛出Email对象的解析错误,即
JSON.parse(emailString);
因为jsonContent
字段中有双引号。
这是一个字符串化JAVA对象的问题,该对象具有已经字符串化的jsonContent
变量。
修复它的一种方法是将jsonContent
变量定义为对象而不是String。
还有其他修复方法吗?
示例电子邮件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"
]
}
}"
}
答案 0 :(得分:1)
你需要转义很多字符串才能把字符串作为字符串。
将json对象存储在需要转义它的json对象中。 所以
"jsonContent": "{
"typeOfMail": "NormalMail",
"normalMail": {
"mailType": "NormalMail",
"paragraphs": [
"Pleasefindthelinktomyrecentpresentation"
]
}
}"
变为
"jsonContent": "{\"typeOfMail\": \"NormalMail\",\"normalMail\":{\"mailType\":\"NormalMail\",\"paragraphs\":[\"Pleasefindthelinktomyrecentpresentation\"]}}"
现在,如果你想在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>
这就是在发送的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\"]}}"
}
现在我不明白你为什么要把jsonContent作为一个字符串,因为你可以把它作为一个对象传递(删除它周围的引号让你得到
"jsonContent": {
"typeOfMail": "NormalMail",
"normalMail": {
"mailType": "NormalMail",
"paragraphs": [
"Pleasefindthelinktomyrecentpresentation"
]
}
}
如果你在javascript中需要它作为字符串,你可以JSON.stringify(json.jsonContent);
来更轻松地获得相同的结果。