简体   繁体   中英

How to format multiline string in JSON

I am trying to POST JSON data to a server-side handler, the expected json object include a json string, the json value is a multiline string. When I POST is as is I get the formatting exception from the server.

Heres is the payload;

{
  "payload": "
 {
            "patient": {
                            "patient.family_name": "Samuel",
                            "patient.given_name": "Owino",
                            "patient.county": "Kilawonk",
                            "patient.location": "Mutwapa",
                            "patient.sub_location": "Kilawonta",
                            "patient.village": "Kilanoi",
                            "patient.phone_number": "0706906138",
                            "patient.medical_record_number": "123456789",
                            "patient.other_identifier_type": "KENYAN NATIONAL ID NUMBER",
                            "patient.other_identifier_value": "32332271",
                            "patient.confirm_identifier_value": "32332271",
                            "patient.sex": "M",
                            "patient.birthdate_estimated": "..."
            },
            "tmp": {
                            "tmp.birthdate_type": "age",
                            "tmp.age_in_years": "21"
            },
            "encounter": {
                            "encounter.location_id": "84",
                            "encounter.provider_id_select": "3356-3",
                            "encounter.provider_id": "3356-3",
                            "encounter.encounter_datetime": "06-11-2017"
            }
}",
  "display": " ",
  "uuid": "             597ec410-996f-494c-ae46-ebb78363f6b1"
}

The server reponse is as follows

{
"error": {
    "message": "[Could not read JSON: Illegal unquoted character ((CTRL-
CHAR, code 13)): has to be escaped using backslash to be included in string 
value\n at [Source: 
org.apache.catalina.connector.CoyoteInputStream@27811c6e; line: 2, column: 
28]; nested exception is org.codehaus.jackson.JsonParseException: Illegal 
unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash 
to be included in string value\n at [Source: 
org.apache.catalina.connector.CoyoteInputStream@27811c6e; line: 2, column: 
28]]",

Please help, thanks in advance.

you have some invalid json in the payload, the string <"payload":, "> should read <"payload":, > and you need to also remove the " at the end of json in the way you build this up. check this uding JSONLint.

valid json below

   {
"payload": {
    "patient": {
        "patient.family_name": "Samuel",
        "patient.given_name": "Owino",
        "patient.county": "Kilawonk",
        "patient.location": "Mutwapa",
        "patient.sub_location": "Kilawonta",
        "patient.village": "Kilanoi",
        "patient.phone_number": "0706906138",
        "patient.medical_record_number": "123456789",
        "patient.other_identifier_type": "KENYAN NATIONAL ID NUMBER",
        "patient.other_identifier_value": "32332271",
        "patient.confirm_identifier_value": "32332271",
        "patient.sex": "M",
        "patient.birthdate_estimated": "..."
    },
    "tmp": {
        "tmp.birthdate_type": "age",
        "tmp.age_in_years": "21"
    },
    "encounter": {
        "encounter.location_id": "84",
        "encounter.provider_id_select": "3356-3",
        "encounter.provider_id": "3356-3",
        "encounter.encounter_datetime": "06-11-2017"
    }
},
"display": " ",
"uuid": "             597ec410-996f-494c-ae46-ebb78363f6b1"

}

this:

"payload": "

is wrong. remove the "" around the object that holds patient

You need to think about escaping the " character in your string, otherwise the parser will think that the string end when it encounters the next matching " .

To escape, use a backslash \\ , like this:

"payload": "{\\"escaped\\":\\"value\\"}"

It can be a bit tricky to understand at first, but json.org has a really nice explanation of how to create valid json.

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.

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