c语言解析json
来源:互联网 发布:十块吧淘宝优站 编辑:程序博客网 时间:2024/05/17 06:19
我使用的是cJSON:http://sourceforge.net/projects/cjson/
先看json的数据结构
c中没有对象,所以json数据是采用链表存储的
比如你有一个json数据
那么你可以
1:讲字符串解析成json结构体。
2:获取某个元素
3:讲json结构体转换成字符串
4:删除
5:构建一个json结构体
先看json的数据结构
c中没有对象,所以json数据是采用链表存储的
- typedef struct cJSON {
- struct cJSON *next,*prev; // 数组 对象数据中用到
- struct cJSON *child; // 数组 和对象中指向子数组对象或值
- int type; // 元素的类型,如是对象还是数组
- char *valuestring; // 如果是字符串
- int valueint; // 如果是数值
- double valuedouble; // 如果类型是cJSON_Number
- char *string; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
- } cJSON;
比如你有一个json数据
- {
- "name": "Jack (\"Bee\") Nimble",
- "format": {
- "type": "rect",
- "width": 1920,
- "height": 1080,
- "interlace": false,
- "frame rate": 24
- }
- }
那么你可以
1:讲字符串解析成json结构体。
- cJSON *root = cJSON_Parse(my_json_string);
2:获取某个元素
- cJSON *format = cJSON_GetObjectItem(root,"format");
- int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
3:讲json结构体转换成字符串
- char *rendered=cJSON_Print(root);
4:删除
- cJSON_Delete(root);
5:构建一个json结构体
- cJSON *root,*fmt;
- root=cJSON_CreateObject();
- cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
- cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
- cJSON_AddStringToObject(fmt,"type", "rect");
- cJSON_AddNumberToObject(fmt,"width", 1920);
- cJSON_AddNumberToObject(fmt,"height", 1080);
- cJSON_AddFalseToObject (fmt,"interlace");
- cJSON_AddNumberToObject(fmt,"frame rate", 24);
0 0
- C语言解析JSON
- c语言解析json
- c语言解析json
- c语言解析json数据
- c语言解析json数据
- c语言解析json数据
- c语言解析json数据
- c语言解析json数据
- Json解析 - c语言库libJsonParser
- 【转】c语言解析json数据
- 在C语言中解析json配置文件
- 在C语言中解析JSON配置文件
- C语言 JSON 的创建和解析
- cJSON库(构建json与解析json字符串)-c语言
- cJSON库(构建json与解析json字符串)-c语言
- cJSON库(构建json与解析json字符串)-c语言
- cJSON库(构建json与解析json字符串)-c语言
- cJSON库(构建json与解析json字符串)-c语言
- 数据挖掘之七种常用的方法
- JSON入门学习
- Majority Element 主要的元素
- 数据分析与可视化工具小谈
- UVa 213 Message Coding
- c语言解析json
- 技术发展瓶颈的突破
- Retrofit2.0 ,OkHttp3完美同步持久Cookie实现免登录(二)
- 补、C++第九次上机实验
- 图像卷积与滤波的一些知识点
- ReactNative-Cocoapods-Swift-Project
- web
- Apache Spark Jobs 性能调优(二)
- 在string.xml文件中定义一个字符串,然后在代码中通过String.format方法来格式化该字符串