在Ubuntu中编译含有JSON的文件出现报错

打印 上一主题 下一主题

主题 1773|帖子 1773|积分 5319

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
 
         在ubuntu中进行JSON相关学习的时间,我发现了一些小问题,决定与各人进行分享,减少踩坑时间出现不必要的时间耗费
   截取部分含有JSON部分的代码进行展示
  1. char *str = "
  2.                                 {  
  3.                                 "title":"JSON Example",  
  4.                                 "author": {  
  5.                                 "name":"John Doe",  
  6.                                 "age": 35,  
  7.                                 "isVerified":true  
  8.                                 },  
  9.                                 "tags":["json", "syntax", "example"],  
  10.                                 "rating": 4.5,  
  11.                                 "isPublished":false,  
  12.                                 "comments": null  
  13.                                 }";
复制代码
按照JSON格式,在引号前边都需要加上 \ 反斜杠
  别的部分的代码与本次的错误分享无关,暂不截图
          打开终端执行 gcc -o json_test json_test.c 下令对本测试代码进行编译
   以下为在编译过程中出现的报错,
  可以发现报错的部分非常的多,而且都明显的指向 \ 反斜杠这一 方面的错误
  

  以下为全部的报错
  1. json_test.c: In function ‘main’:
  2. json_test.c:6:14: error: stray ‘\’ in program
  3.   char *str = "
  4.               ^
  5. json_test.c:6:15: warning: missing terminating " character
  6.   char *str = "
  7.                ^
  8. json_test.c:6:15: error: missing terminating " character
  9. json_test.c:8:5: error: stray ‘\’ in program
  10.      "title":"JSON Example",
  11.      ^
  12. json_test.c:8:6: warning: missing terminating " character
  13.      "title":"JSON Example",
  14.       ^
  15. json_test.c:8:6: error: missing terminating " character
  16.      "title":"JSON Example",
  17.       ^~~~~~~~~~~~~~~~~~~~~~~~~~  
  18. json_test.c:9:5: error: stray ‘\’ in program
  19.      "author": {
  20.      ^
  21. json_test.c:9:6: warning: missing terminating " character
  22.      "author": {
  23.       ^
  24. json_test.c:9:6: error: missing terminating " character
  25.      "author": {
  26.       ^~~~~~~~~~~~  
  27. json_test.c:10:5: error: stray ‘\’ in program
  28.      "name":"John Doe",
  29.      ^
  30. json_test.c:10:6: warning: missing terminating " character
  31.      "name":"John Doe",
  32.       ^
  33. json_test.c:10:6: error: missing terminating " character
  34.      "name":"John Doe",
  35.       ^~~~~~~~~~~~~~~~~~~~~  
  36. json_test.c:11:5: error: stray ‘\’ in program
  37.      "age": 35,
  38.      ^
  39. json_test.c:11:6: warning: missing terminating " character
  40.      "age": 35,
  41.       ^
  42. json_test.c:11:6: error: missing terminating " character
  43.      "age": 35,
  44.       ^~~~~~~~~~~  
  45. json_test.c:12:5: error: stray ‘\’ in program
  46.      "isVerified":true
  47.      ^
  48. json_test.c:12:6: warning: missing terminating " character
  49.      "isVerified":true
  50.       ^
  51. json_test.c:12:6: error: missing terminating " character
  52.      "isVerified":true
  53.       ^~~~~~~~~~~~~~~~~~  
  54. json_test.c:7:5: error: empty scalar initializer
  55.      {
  56.      ^
  57. json_test.c:7:5: note: (near initialization for ‘str’)
  58. json_test.c:14:5: error: stray ‘\’ in program
  59.      "tags":["json", "syntax", "example"],
  60.      ^
  61. json_test.c:14:6: warning: missing terminating " character
  62.      "tags":["json", "syntax", "example"],
  63.       ^
  64. json_test.c:14:6: error: missing terminating " character
  65.      "tags":["json", "syntax", "example"],
  66.       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  67. json_test.c:15:5: error: stray ‘\’ in program
  68.      "rating": 4.5,
  69.      ^
  70. json_test.c:15:6: warning: missing terminating " character
  71.      "rating": 4.5,
  72.       ^
  73. json_test.c:15:6: error: missing terminating " character
  74.      "rating": 4.5,
  75.       ^~~~~~~~~~~~~~~  
  76. json_test.c:16:5: error: stray ‘\’ in program
  77.      "isPublished":false,
  78.      ^
  79. json_test.c:16:6: warning: missing terminating " character
  80.      "isPublished":false,
  81.       ^
  82. json_test.c:16:6: error: missing terminating " character
  83.      "isPublished":false,
  84.       ^~~~~~~~~~~~~~~~~~~~~  
  85. json_test.c:17:5: error: stray ‘\’ in program
  86.      "comments": null
  87.      ^
  88. json_test.c:17:6: warning: missing terminating " character
  89.      "comments": null
  90.       ^
  91. json_test.c:17:6: error: missing terminating " character
  92.      "comments": null
  93.       ^~~~~~~~~~~~~~~~~  
  94. json_test.c:18:5: error: expected identifier or ‘(’ before ‘}’ token
  95.      }";
  96.      ^
  97. json_test.c:18:6: error: stray ‘\’ in program
  98.      }";
  99.       ^
  100. json_test.c:18:7: warning: missing terminating " character
  101.      }";
  102.        ^
  103. json_test.c:18:7: error: missing terminating " character
  104.      }";
  105.        ^~
复制代码
对于这个问题的办理实在不难,只需要在每一行的后边加上连词符号 \ 即可,将多行拼成1行 
 代码做出修改后如下:
  1. char *str = "\
  2.                                 {  \
  3.                                 "title":"JSON Example",\
  4.                                 "author": {  \
  5.                                                         "name":"John Doe",\
  6.                                                         "age": 35,\
  7.                                                         "isVerified":true\
  8.                                                         }, \
  9.                                 "tags":["json", "syntax", "example"],\
  10.                                 "rating": 4.5,\
  11.                                 "isPublished":false,\
  12.                                 "comments": null\
  13.                                 }";
复制代码
再次对其进行编译,之前的报错根本消失   
   但是,我们发现了别的一个告诫错误
  

  1. json_test.c: In function ‘main’:
  2. json_test.c:8:32: warning: backslash and newline separated by space
  3.      "title":"JSON Example",\
  4.                                  
  5. json_test.c:14:51: warning: backslash and newline separated by space
  6.      "tags":["json", "syntax", "example"], \
  7.                                                    
  8. /tmp/ccFimHV4.o: In function `main':
  9. json_test.c:(.text+0x22): undefined reference to `cJSON_Parse'
  10. json_test.c:(.text+0x39): undefined reference to `cJSON_GetObjectItem'
  11. collect2: error: ld returned 1 exit status
复制代码
        同时回到text editor代码中,发现代码中反斜杠居然会出现两种颜色
  

  这个现象很有意思,一开始并没有留意这是为什么,后边结合告诫
  在这几个颜色差别的反斜杠之间瞎点,发现一个小问题
  这些差别于大众的颜色的反斜杠,后边带有空格,其他的没有
  尝试着将后边的空格去掉之后,再次进行编译,前边的告诫错误荡然无存
  


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表