簡體   English   中英

無法在Groovy / Java中編寫JSON字符串

[英]Can't write JSON string in Groovy/Java

因此,我嘗試將以下JSON編寫為Java字符串,但遇到我不理解的錯誤:

    String simpleAPI_MessageInJSON = "{                                          " +
                                 "       \"action\": \"add\",                    " +
                                 "       \"destinations\": {                     " +
                                 "           \"cache\": 1,                       " +
                                 "           \"batches\": 1                      " +
                                 "        },                                     " +
                                 "       \"payload\": {                          " +
                                 "           \"object_type\": \"profile\",       " +
                                 "           \"object_id\": 366334,              " +
                                 "        }                                      " +
                                 "    }                                          ";

錯誤:

com.fasterxml.jackson.core.JsonParseException:意外的字符('}'(代碼125)):期望雙引號開頭字段名稱

您還有一個逗號:

"           \"object_id\": 366334,              " +

應該:

"           \"object_id\": 366334              " +
//                              /\ Extra comma was there.

錯誤消息顯示:

com.fasterxml.jackson.core.JsonParseException:意外的字符('}'(代碼125)):期望雙引號開頭字段名稱

這不是很清楚,但是我們可以確定這是某種語法錯誤,並且具有行號。 通過查看行號,可以發現語法錯誤。

如果使用groovy來提高可讀性,通常使用多行字符串會更容易:

String simpleAPI_MessageInJSON = '''{                                          
                                   |    "action": "add",
                                   |    "destinations": {
                                   |        "cache": 1,
                                   |        "batches": 1
                                   |    },
                                   |    "payload": {
                                   |        "object_type": "profile",
                                   |        "object_id": 366334
                                   |    }
                                   |}'''.stripMargin()

@Annubian Noob

奇怪,適用於常規

import groovy.json.JsonSlurper

String simpleAPI_MessageInJSON = "{                                          " +
                                 "       \"action\": \"add\",                    " +
                                 "       \"destinations\": {                     " +
                                 "           \"cache\": 1,                       " +
                                 "           \"batches\": 1                      " +
                                 "        },                                     " +
                                 "       \"payload\": {                          " +
                                 "           \"object_type\": \"profile\",       " +
                                 "           \"object_id\": 366334,              " +
                                 "        }                                      " +
                                 "    }  "
def parsed = new JsonSlurper().parseText(simpleAPI_MessageInJSON)
assert parsed.action == "add"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM