JSON -- c语言,数据交换
2015-10-12 20:12
330 查看
一、c语言获取json中的数据
先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h。
下载地址:http://download.csdn.net/detail/wei_xiaox126/9174977
二、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'sname string, if this item is the child of, or is in the list of subitems of anobject.
} cJSON;
三、cJSON使用
1、字符串解析成json结构体
1):讲字符串解析成json结构体。
cJSON *root = cJSON_Parse(my_json_string);
2):获取某个元素
cJSON *format =cJSON_GetObjectItem(root,"format");
int framerate =cJSON_GetObjectItem(format,"frame rate")->valueint;
int framerate =cJSON_GetObjectItem(format,"frame rate")->valueint;
3):讲json结构体转换成字符串
char *rendered=cJSON_Print(root);
4):删除
cJSON_Delete(root);
2、构建一个json结构体
1)示例:
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
2)代码:
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,"framerate", 24)
out =cJSON_Print(root);
printf("%s\n",out);
cJSON_Delete(root);
free(out);
先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h。
下载地址:http://download.csdn.net/detail/wei_xiaox126/9174977
二、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'sname string, if this item is the child of, or is in the list of subitems of anobject.
} cJSON;
三、cJSON使用
1、字符串解析成json结构体
1):讲字符串解析成json结构体。
cJSON *root = cJSON_Parse(my_json_string);
2):获取某个元素
cJSON *format =cJSON_GetObjectItem(root,"format");
int framerate =cJSON_GetObjectItem(format,"frame rate")->valueint;
int framerate =cJSON_GetObjectItem(format,"frame rate")->valueint;
3):讲json结构体转换成字符串
char *rendered=cJSON_Print(root);
4):删除
cJSON_Delete(root);
2、构建一个json结构体
1)示例:
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
2)代码:
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,"framerate", 24)
out =cJSON_Print(root);
printf("%s\n",out);
cJSON_Delete(root);
free(out);
相关文章推荐
- C++ STL学习笔记六 bit_vector位向量容器
- C++ STL学习笔记五 slist单向链表容器
- C++ STL学习笔记四 list双向链表容器
- C++ STL学习笔记三 deque双端队列容器
- C++ STL学习笔记一
- C++ STL学习笔记二 vector向量容器
- c语言实现模拟发送报文
- C++ 单链表操作
- 让程序在崩溃时体面的退出之CallStack
- 让程序在崩溃时体面的退出之Unhandled Exception
- 让程序在崩溃时体面的退出之终极解决方案(SEH+Dump+Unhandled Exception Filter)
- 让程序在崩溃时体面的退出之SEH+Dump文件
- 让程序在崩溃时体面的退出之Dump文件
- 让程序在崩溃时体面的退出之SEH
- 第五章C++Primer表达式
- C++STL之multiset多重集合容器
- 转载一份C++线程池的代码,非常实用
- 从C++prime的习题开始做起
- Call C# code from C++
- Visual C++ 64 位迁移的常见问题