当前位置: 首页 > article >正文

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

 

         在ubuntu中进行JSON相关学习的时候,我发现了一些小问题,决定与大家进行分享,减少踩坑时候出现不必要的时间耗费

截取部分含有JSON部分的代码进行展示

char *str = "
				{  
				\"title\":\"JSON Example\",  
				\"author\": {  
				\"name\":\"John Doe\",  
				\"age\": 35,  
				\"isVerified\":true  
				},  
				\"tags\":[\"json\", \"syntax\", \"example\"],  
				\"rating\": 4.5,  
				\"isPublished\":false,  
				\"comments\": null  
				}";

按照JSON格式,在引号前边都需要加上 \ 反斜杠

其余部分的代码与本次的错误分享无关,暂不截图

        打开终端执行 gcc -o json_test json_test.c 命令对本测试代码进行编译

以下为在编译过程中出现的报错,

可以发现报错的部分非常的多,而且都明显的指向 \ 反斜杠这一 方面的错误

以下为全部的报错

json_test.c: In function ‘main’:
json_test.c:6:14: error: stray ‘\’ in program
  char *str = \"
              ^
json_test.c:6:15: warning: missing terminating " character
  char *str = \"
               ^
json_test.c:6:15: error: missing terminating " character
json_test.c:8:5: error: stray ‘\’ in program
     \"title\":\"JSON Example\",
     ^
json_test.c:8:6: warning: missing terminating " character
     \"title\":\"JSON Example\",
      ^
json_test.c:8:6: error: missing terminating " character
     \"title\":\"JSON Example\",
      ^~~~~~~~~~~~~~~~~~~~~~~~~~  
json_test.c:9:5: error: stray ‘\’ in program
     \"author\": {
     ^
json_test.c:9:6: warning: missing terminating " character
     \"author\": {
      ^
json_test.c:9:6: error: missing terminating " character
     \"author\": {
      ^~~~~~~~~~~~  
json_test.c:10:5: error: stray ‘\’ in program
     \"name\":\"John Doe\",
     ^
json_test.c:10:6: warning: missing terminating " character
     \"name\":\"John Doe\",
      ^
json_test.c:10:6: error: missing terminating " character
     \"name\":\"John Doe\",
      ^~~~~~~~~~~~~~~~~~~~~  
json_test.c:11:5: error: stray ‘\’ in program
     \"age\": 35,
     ^
json_test.c:11:6: warning: missing terminating " character
     \"age\": 35,
      ^
json_test.c:11:6: error: missing terminating " character
     \"age\": 35,
      ^~~~~~~~~~~  
json_test.c:12:5: error: stray ‘\’ in program
     \"isVerified\":true
     ^
json_test.c:12:6: warning: missing terminating " character
     \"isVerified\":true
      ^
json_test.c:12:6: error: missing terminating " character
     \"isVerified\":true
      ^~~~~~~~~~~~~~~~~~  
json_test.c:7:5: error: empty scalar initializer
     {
     ^
json_test.c:7:5: note: (near initialization for ‘str’)
json_test.c:14:5: error: stray ‘\’ in program
     \"tags\":[\"json\", \"syntax\", \"example\"],
     ^
json_test.c:14:6: warning: missing terminating " character
     \"tags\":[\"json\", \"syntax\", \"example\"],
      ^
json_test.c:14:6: error: missing terminating " character
     \"tags\":[\"json\", \"syntax\", \"example\"],
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
json_test.c:15:5: error: stray ‘\’ in program
     \"rating\": 4.5,
     ^
json_test.c:15:6: warning: missing terminating " character
     \"rating\": 4.5,
      ^
json_test.c:15:6: error: missing terminating " character
     \"rating\": 4.5,
      ^~~~~~~~~~~~~~~  
json_test.c:16:5: error: stray ‘\’ in program
     \"isPublished\":false,
     ^
json_test.c:16:6: warning: missing terminating " character
     \"isPublished\":false,
      ^
json_test.c:16:6: error: missing terminating " character
     \"isPublished\":false,
      ^~~~~~~~~~~~~~~~~~~~~  
json_test.c:17:5: error: stray ‘\’ in program
     \"comments\": null
     ^
json_test.c:17:6: warning: missing terminating " character
     \"comments\": null
      ^
json_test.c:17:6: error: missing terminating " character
     \"comments\": null
      ^~~~~~~~~~~~~~~~~  
json_test.c:18:5: error: expected identifier or ‘(’ before ‘}’ token
     }\";
     ^
json_test.c:18:6: error: stray ‘\’ in program
     }\";
      ^
json_test.c:18:7: warning: missing terminating " character
     }\";
       ^
json_test.c:18:7: error: missing terminating " character
     }\";
       ^~

对于这个问题的解决其实不难,只需要在每一行的后边加上连词符号 \ 即可,将多行拼成1行 

 代码做出修改后如下:

char *str = "\
				{  \
				\"title\":\"JSON Example\",\ 
				\"author\": {  \
							\"name\":\"John Doe\",\
							\"age\": 35,\
							\"isVerified\":true\
							}, \
				\"tags\":[\"json\", \"syntax\", \"example\"],\ 
				\"rating\": 4.5,\
				\"isPublished\":false,\
				\"comments\": null\
				}";

再次对其进行编译,之前的报错基本消失   

但是,我们发现了另外一个警告错误

json_test.c: In function ‘main’:
json_test.c:8:32: warning: backslash and newline separated by space
     \"title\":\"JSON Example\",\
                                 
json_test.c:14:51: warning: backslash and newline separated by space
     \"tags\":[\"json\", \"syntax\", \"example\"], \
                                                    
/tmp/ccFimHV4.o: In function `main':
json_test.c:(.text+0x22): undefined reference to `cJSON_Parse'
json_test.c:(.text+0x39): undefined reference to `cJSON_GetObjectItem'
collect2: error: ld returned 1 exit status

        同时回到text editor代码中,发现代码中反斜杠居然会出现两种颜色

这个现象很有意思,一开始并没有注意这是为什么,后边结合警告

在这几个颜色不同的反斜杠之间瞎点,发现一个小问题

这些不同于大众的颜色的反斜杠,后边带有空格,其他的没有

尝试着将后边的空格去掉之后,再次进行编译,前边的警告错误荡然无存


http://www.kler.cn/news/308552.html

相关文章:

  • 【前后端】大文件切片上传
  • 网络安全学习(一)初识kali
  • 【JavaEE初阶】多线程(5 单例模式 \ 阻塞队列)
  • 构建基于 Feign 的微服务:从 Eureka 到负载均衡的实践 --day05
  • 微信支付开发-前端api实现
  • 大模型笔记03--快速体验dify
  • HTTP的强制缓存和协商缓存有什么区别和联系?
  • 《使用 LangChain 进行大模型应用开发》学习笔记(三)
  • 行人动作行为识别系统源码分享
  • LLamaindex基本使用
  • MYSQL数据库基础篇——DDL
  • 第7篇:【系统分析师】计算机网络
  • openwrt wsdd模块介绍
  • C++(重载)
  • Skytower
  • 第二百三十五节 JPA教程 - JPA Lob列示例
  • k8s(kubernetes)的PV / PVC / StorageClass(理论+实践)
  • 数据库的操作:SQL运算符(算法/比较/逻辑/位)
  • 【大模型专栏—进阶篇】智能对话全总结
  • 力扣100题——动态规划(二)
  • React Native防止重复点击
  • 详解 Pandas 的透视表函数
  • PHP智能化云端培训考试系统小程序源码
  • YOLOv5 Detect.py 改变检测框box线条的粗细,隐藏检测框的检测信息,只显示检测框box
  • PHP在现代Web开发中的高效应用与最佳实践
  • Linux杂项知识
  • 深入解析:如何通过网络命名空间跟踪单个进程的网络活动(C/C++代码实现)
  • Vue3.0组合式API:setup()函数
  • SpringBoot 消息队列RabbitMQ在代码中声明 交换机 与 队列使用注解创建
  • Linux | 进程间通信:管道、消息队列、共享内存与信号量