如何在JSON中格式化多行字符串

时间:2017-11-07 14:18:22

标签: java json rest spring-mvc

我正在尝试将JSON数据发布到服务器端处理程序,预期的json对象包含json字符串,json值是多行字符串。当我按原样POST时,我从服务器获得格式化异常。

Heres是有效载荷;

{
  "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"
}

服务器响应如下

{
"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]]",

请提前帮助,谢谢。

3 个答案:

答案 0 :(得分:1)

你在有效载荷中有一些无效的json,字符串<“payload”:,“>应该读取<”payload“:,>并且你还需要删除”在json的末尾你建立起来的方式。检查这个你的JSONLint。

下面有效的json

   {
"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"

}

答案 1 :(得分:0)

这样:

""

错了。删除包含patient

的对象周围的library ieee; use ieee.numeric_bit.all; entity bin2bcd is port (bin : in bit_vector(3 downto 0) := "0000"; clk : in bit; bcdout : out bit_vector(4 downto 0) := "00000"); end bin2bcd; architecture bin2bcdarch of bin2bcd is begin process(clk) variable gt9 : bit; variable temp : bit_vector(3 downto 0) := "0110"; variable bcdout_temp : bit_vector(4 downto 0); begin if clk'event and clk = '1' then gt9 := bin(3) and(bin(2) or bin(1)); if gt9 = '1' then bcdout_temp := ('0' & bin) + ('0' & temp); else bcdout_temp := ('0' & bin); end if; end if; bcdout <= bcdout_temp; end process; end bin2bcdarch;

答案 2 :(得分:0)

您需要考虑转义字符串中的&#34; 字符,否则解析器会在遇到下一个匹配&#34;

要逃避,请使用反斜杠 \ ,如下所示:

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

一开始理解起来可能有点棘手,但json.org对如何创建有效的json有一个非常好的解释。