
json-c库
json-c 库中是在嵌入式开发中常用的库。
因为很多地方都以json数据数据交互协议, 尤其嵌入式web数据交互时通常会用到json格式, 因此如果需要在产品端进行json数据解析 , json-c 是一个比较不错的选择。
API接口
json-c库中有一个json对象 :jsonobj
.它会将一个json文件解析为一个json对象.
使用方式:
1.解析json文件,获取一个解析后的json对象.
2.访问对应的key值.
3.使用后,释放json对象.
解析json文件
方式一:将json格式的字符串转成json对象
json_object* json_tokener_parse(const char *str);
方式二:具有json格式文本内容的文本文件转化为json对象
json_object* json_object_from_file(const char *filename);
访问对应的key值
extern json_bool json_object_object_get_ex(struct json_object* obj,const char *key,struct json_object **value);
释放json文件
Void json_object_put(struct json_object * this);
其他常用API
(1)创建一个空的json_type_object类型JSON对象:
struct json_object * json_object_new_object();
(2)创建一个空的json_type_array类型JSON数组值对象:
struct json_object * json_object_new_array();
(3)从json中按名字取一个对象:
struct json_object * json_object_object_get(struct json_object * json,char *name);
(4)减少对象引用次数一次,当减少到0就释放(free)资源:
Void json_object_put(struct json_object * this);
(5)将json_object内容转换json格式字符串,其中可能含有转义符:
char * json_object_to_json_string(struct json_object * this);
(6)添加一个对象域到json对象中:
void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);
(7)删除key值json对象:
void json_object_object_del(struct json_object* obj, char *key);
(8)得到json对象数组的长度:
int json_object_array_length(struct json_object *obj);
(9)添加一元素在json对象数组末端:
extern int json_object_array_add(struct json_object *obj, struct json_object *val);
(10)在指定的json对象数组下标插入或替换一个json对象元素:
int json_object_array_put_idx(struct json_object *obj, int idx, struct json_object *val);
(11)从数组中,按下标取JSON值对象:
struct json_object * json_object_array_get_idx(struct json_object * json_array,int i);
(12)得到json_object的类型:
enum json_type json_object_get_type(struct json_object * this )
参考代码[转载]
#include <stdio.h>
#include <json-c/json.h>
/*
{
Name: haha,
Id: 101,
Age: 21,
info:{
number: 1,
score: 91,
type: 2,
params: ""
}
}
*/
// gcc json-c_parse_string.c -ljson-c
int main(int argc, char const *argv[])
{
/* Declaring the json data's in json format. */
char buf[] = "{ \"Name\": \"haha\", \"Id\": 101, \"Age\": 21, \"info\": { \"number\": 1, \"score\": 91, \"type\": 2, \"params\": \"w\" } }";
/* Declaring the json_object. To pass the Json string to the newly created json_object. */
json_object *new_obj = json_tokener_parse(buf);
if (!new_obj)
return -1;
json_object *val_obj = NULL;
json_object *result = NULL;
const char *str = NULL;
/* To get the data's then we have to get to the specific node by using the below function. */
if( json_object_object_get_ex(new_obj, "Name", &val_obj) ) {
str = json_object_get_string(val_obj);
printf("Name: %s\n", str);
}
if( json_object_object_get_ex(new_obj, "Id", &val_obj) ) {
str = json_object_get_string(val_obj);
printf("Id: %s\n", str);
}
if( json_object_object_get_ex(new_obj, "Age", &val_obj) ) {
str = json_object_get_string(val_obj);
printf("Age: %s\n", str);
}
if( json_object_object_get_ex(new_obj, "info", &val_obj) ) {
if( json_object_object_get_ex(val_obj, "number", &result) ) {
printf("\tinfo -> number: %d\n", json_object_get_int(result));
}
if( json_object_object_get_ex(val_obj, "score", &result) ) {
printf("\tinfo -> score: %d\n", json_object_get_int(result));
}
if( json_object_object_get_ex(val_obj, "type", &result) ) {
printf("\tinfo -> type: %d\n", json_object_get_int(result));
}
if( json_object_object_get_ex(val_obj, "params", &result) ) {
printf("\tinfo -> params: %s\n", json_object_get_string(result));
}
}
json_object_put(new_obj); // to return the pointer to its originalobjects
return 0;
}
网友评论