c语言解析json数据
来源:互联网 发布:阴茎毛囊炎 知乎 编辑:程序博客网 时间:2024/05/16 17:04
本文转载自: http://hi.baidu.com/%C1%B5%D5%BD%D0%A1%CC%EC/blog/item/f70b39145425055f21a4e96b.html
我使用的是cJSON:http://sourceforge.net/projects/cjson/
先看json的数据结构
c中没有对象,所以json数据是采用链表存储的
C代码
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数据
Javascript代码
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
那么你可以
1:讲字符串解析成json结构体。
C代码
cJSON *root = cJSON_Parse(my_json_string);
cJSON *root = cJSON_Parse(my_json_string);
2:获取某个元素
C代码
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
3:讲json结构体转换成字符串
C代码
char *rendered=cJSON_Print(root);
char *rendered=cJSON_Print(root);
4:删除
C代码
cJSON_Delete(root);
cJSON_Delete(root);
5:构建一个json结构体
C代码
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);
- c语言解析json数据
- c语言解析json数据
- c语言解析json数据
- c语言解析json数据
- c语言解析json数据
- 【转】c语言解析json数据
- C语言解析JSON
- c语言解析json
- c语言解析json
- JSON -- c语言,数据交换
- OC语言学习14-JSON数据解析
- Json解析 - c语言库libJsonParser
- 在C语言中解析json配置文件
- 在C语言中解析JSON配置文件
- C语言 JSON 的创建和解析
- cJson的用法(一) -C语言解析/生成 json数据
- C#json数据解析和xml2json
- Objective-C Json格式数据解析
- Android基础-AppWidget入门
- FFMPEG源码分析:avformat_open_input()(媒体打开函数)
- cocos2d-x CCAction scrollMap
- vs编译器选项
- 安装win7 SP1后或者安装其他补丁后,在应用更新操作第XXX个文件时候遇到致命错误的解决办法
- c语言解析json数据
- 为基于Wordpress的网站添加网站Icon
- 详解Android源码的编译
- 总结教训:stm32f4-Discovery驱动三星s6d1121液晶程序
- FTP主动模式和被动模式
- CISCO数据中心虚拟化之vPC技术和配置
- 关于 Java 中 finally 语句块的深度辨析
- C#制作外挂常用API。。自己存着。记录
- php验证码 GD库生成验证码