So I am trying to write the following JSON as a Java string but getting an error I don't understand:
String simpleAPI_MessageInJSON = "{ " +
" \"action\": \"add\", " +
" \"destinations\": { " +
" \"cache\": 1, " +
" \"batches\": 1 " +
" }, " +
" \"payload\": { " +
" \"object_type\": \"profile\", " +
" \"object_id\": 366334, " +
" } " +
" } ";
ERROR:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
You have an extra comma:
" \"object_id\": 366334, " +
Should be:
" \"object_id\": 366334 " +
// /\ Extra comma was there.
The error message says:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
This isn't very clear, but we can make out that it's a syntax error of some kind, and it has a line number. By looking around the line number, you can find the syntax error.
It's generally easier to use multi-line strings if you are using groovy for improved readability:
String simpleAPI_MessageInJSON = '''{
| "action": "add",
| "destinations": {
| "cache": 1,
| "batches": 1
| },
| "payload": {
| "object_type": "profile",
| "object_id": 366334
| }
|}'''.stripMargin()
@Annubian Noob
Strange, works with groovy:
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.