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

news/2024/9/18 23:17:36 标签: linux, 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.niftyadmin.cn/n/5664683.html

相关文章

QGis二次开发 —— 3、程序加载栅格tif与矢量shp文件可进行切换控制,可进行导出/导入工程(附源码)

效果 功能说明 软件可同时加载.tif栅格图片与.shp矢量图片、加载图片后可进行自由切换查看图层、可对加载的图片进行关闭 关闭后清空图层、可对加载的图片进行导出.qgs的QGIS工程、可对.qgs的QGis工程导入并导入后可进行自由切换查看图层。 源码 注意: 在加载tif栅格文件后会在…

基于SpringBoot+Vue的房屋租赁平台

作者:计算机学姐 开发技术:SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等,“文末源码”。 专栏推荐:前后端分离项目源码、SpringBoot项目源码、SSM项目源码 系统展示 【2025最新】基于JavaSpringBootVueMySQL的…

Mobile net V系列详解 理论+实战(2)

Mobilenet 系列 实践部分一、数据集介绍二、模型整体框架三、模型代码详解四、总结 实践部分 本章针对实践通过使用pytorch一个实例对这部分内容进行吸收分析。本章节采用的源代码在这里感兴趣的读者可以自行下载操作。 一、数据集介绍 可以看到数据集本身被存放在了三个文件…

Day02Day03

1. 为什么拦截器不会去拦截/admin/login上,是因为在SpringMvc中清除了这种可能。 2.使用自己定义注解,实现AOP(insert ,update) 3.使用update最好使用动态语句,可以使用多次 4.使用阿里云的OSS存储。用common类 5.在写…

动手学深度学习(pytorch)学习记录28-使用块的网络(VGG)[学习记录]

目录 VGG块VGG网络训练模型 VGG块 定义了一个名为vgg_block的函数来实现一个VGG块 import torch from torch import nn from d2l import torch as d2ldef vgg_block(num_convs, in_channels, out_channels):layers []for _ in range(num_convs):layers.append(nn.Conv2d(in_…

Go 1.19.4 路径和目录-Day 15

1. 路径介绍 存储设备保存着数据,但是得有一种方便的模式让用户可以定位资源位置,操作系统采用一种路径字符 串的表达方式,这是一棵倒置的层级目录树,从根开始。 相对路径:不是以根目录开始的路径,例如 a/b…

ant-design表格自动合并相同内容的单元格

表格自动合并相同内容的单元格 合并hooks import { TableColumnProps } from antdexport const useAutoMergeTableCell <T extends object>(dataSource: Array<T>,columns: Array<TableColumnProps> | Array<keyof T> ): Map<keyof T, Array<…

【运维方案】软件运维服务方案(word)

1.项目情况 2.服务简述 2.1服务内容 2.2服务方式 2.3服务要求 2.4服务流程 2.5工作流程 2.6业务关系 2.7培训 3.资源提供 3.1项目组成员 3.2服务保障 进主页学习更多获取更多资料&#xff5e;