1

I am getting this weird error below

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

CODE:

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;
 }

I understood error is about char * string line. But don't know how to fix it.

Naroju
  • 2,637
  • 4
  • 25
  • 44
  • Don't you need to escape? – Sourav Ghosh May 23 '16 at 14:26
  • I tried escaping inner double quotes by adding "\" infront of each ,I am getting this error `error: stray "\" in program` – Naroju May 23 '16 at 14:38
  • each of the parts of the string should be ended with something similar to "\n" \ Notice the trailing back slash. To make it easy for you, suggest writing a table of pointers to char strings, Then using a vary long string buffer, pass each line of the json commands into that buffer using one call to `strcpy()` and and several calls to `strcat()` be sure to include all the necessary escaped double quotes and include `\n` where ever a newline is needed, including at the end of the accumulated string. – user3629249 May 24 '16 at 06:54

2 Answers2

2

You have to:

  1. to escape " char because of it is a special char used to define a C-String literal.
  2. For multi line strings you must define each line as a single C-String using "" for each one

So, the resulting code is

  char * string = "{"
                  "\"sender\" : \"joys of programming\","
                  "\"receiver\": [ \"123\","
                                  "\"345\","
                                  "\"654\","
                                  "\"432\""
                                "]"
                 "}";
LPs
  • 16,045
  • 8
  • 30
  • 61
1

You need to use escape character when using quotations marks inside quotation marks.

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

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

                "}";

This is going to do it.

Mirakurun
  • 4,859
  • 5
  • 16
  • 32
  • I tried escaping inner double quotes by adding "\" infront of each ,I am getting this error `error: stray "\" in program` – Naroju May 23 '16 at 14:39
  • This is what I get as an output when I print it out: { "sender" : "joys of programming","receiver": [ "123","345","654","432"]} – Mirakurun May 23 '16 at 14:44