簡體   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