1

我在下面收到这个奇怪的错误

json.c:81:19: warning: missing terminating " character [enabled by  default]
json.c:81:3: error: missing terminating " character
json.c:82:32: error: expected ‘,’ or ‘;’ before ‘:’ token
json.c:90:22: warning: missing terminating " character [enabled by default]
json.c:90:21: error: missing terminating " character

代码:

int main()
{
  char * string = "{
                  "sender" : "joys of programming",

                   "receiver": [ "123",
                                 "345",
                                 "654",
                                 "432"
                               ]

                }";
 printf("JSON string: %sn", string);
 json_object * jobj = json_tokener_parse(string);
 json_parse(jobj);
 return 0;
 }

我理解错误是关于char * string线的。但不知道如何解决。

4

2 回答 2

2

你必须:

  1. 转义"char 因为它是用于定义 C-String 文字的特殊字符。
  2. 对于多行字符串,您必须将每一行定义为单个 C 字符串,使用""for each one

所以,结果代码是

  char * string = "{"
                  "\"sender\" : \"joys of programming\","
                  "\"receiver\": [ \"123\","
                                  "\"345\","
                                  "\"654\","
                                  "\"432\""
                                "]"
                 "}";
于 2016-05-23T14:39:48.080 回答
1

在引号内使用引号时需要使用转义字符。

  char * string = "{ "
                  "\"sender\" : \"joys of programming\"," 

                   "\"receiver\": [ \"123\","
                                   "\"345\","
                                   "\"654\","
                                   "\"432\""
                               "]"

                "}";

这是要做到的。

于 2016-05-23T14:35:32.840 回答