繁体   English   中英

JSON.parse Javascript中对Java对象抛出错误进行字符串化

[英]Stringifying a Java Object throwing Error in JSON.parse Javascript

我定义了一个具有以下详细信息的类电子邮件:
Email

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

在上面的类中, jsonContent变量加载了经过分层处理的json对象。
创建电子邮件对象后,我将对整个电子邮件对象进行字符串化并发送给客户端。

我需要在客户端中解析电子邮件对象并在UI中呈现它。
但是它会为客户端中的电子邮件对象引发解析错误,即

JSON.parse(emailString);

因为jsonContent字段内有双引号。
这是对具有已字符串化的jsonContent变量的JAVA对象进行字符串化的问题。

修复它的一种方法是将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"
    ]
  }
}"
}

您将需要转义许多字符串才能将东西作为字符串获取。

要将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); 为了更容易获得相同的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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