Json-c的使用

1.使用json-c前,先在自己的linux上安装json-c的库。
2.函数说明

struct json_object * json_object_new_object();

说明:
创建个空的json_type_object类型JSON对象。

struct json_object* json_object_new_boolean(Boolean b);

说明:
创建个json_type_boolean值类型json对象

Boolean json_object_get_boolean(struct json_object *obj);

说明:
从json对象中boolean值类型得到boolean值

struct json_object * json_object_new_array();

说明:

创建个空的json_type_array类型JSON数组值对象

struct json_object * json_tokener_parse(char *str);

说明:

由str里的JSON字符串生成JSON对象,str是json_object_to_json_string() 生成的。

参数:

str – json字符串

struct json_object * json_object_object_get(struct json_object * json,char *name);

说明:

从json中按名字取一个对象。

参数:

json – json对象

name - json域名字

struct json_object * * json_object_get(struct json_object * this)

说明:

增加对象引用计数。使用c库最关心的是内存谁来分配, 谁来释放. jsonc的内存管理方式, 是基于引用计数的内存树(链), 如果把一个struct json_object 对象a, add到另一个对象b上, 就不用显式的释放(json_object_put) a了, 相当于把a挂到了b的对象树上, 释放b的时候, 就会释放a. 当a既add到b上, 又add到对象c上时会导致a被释放两次(double free), 这时可以增加a的引用计数(调用函数json_object_get(a)), 这时如果先释放b, 后释放c, 当释放b时, 并不会真正的释放a, 而是减少a的引用计数为1, 然后释放c时, 才真正释放a.

参数:

this – json对象

Void json_object_put(struct json_object * this)

说明:

减少对象引用次数一次,当减少到0就释放(free)资源

参数:

this – json对象

Int json_object_is_type(struct json_object * this, enum json_type type)

说明:

检查json_object是json的某个类型

参数:

this: json_object 实例
type: json_type_boolean,json_type_double, json_type_int, json_type_object, json_type_array, json_type_string

enum json_type json_object_get_type(struct json_object * this )

说明:

得到json_object的类型。

参数:

this – json对象

char * json_object_to_json_string(struct json_object * this)

说明:

将json_object内容转换json格式字符串,其中可能含有转义符。

参数:

this – json对象

返回值:
Json格式字符串

void json_object_object_add(struct json_object* obj, char *key, struct json_object *val);

说明:
添加个对象域到json对象中

参数:

Obj – json对象

key – 域名字

val – json值对象

void json_object_object_del(struct json_object* obj, char *key);

说明:

删除key值json对象

参数:

ob j – json对象

key – 域名字

int json_object_array_length(struct json_object *obj);

说明:

得到json对象数组的长度。

参数:

ob j – json数组值对象

extern int json_object_array_add(struct json_object *obj,struct json_object *val);

说明:

添加一元素在json对象数组末端

参数:

ob j – json数组值对象

val – json值对象

int json_object_array_put_idx(struct json_object *obj, int idx,struct json_object *val);

说明:

在指定的json对象数组下标插入或替换一个json对象元素。

参数:

ob j – json数组值对象

val – json值对象

dx – 数组下标

struct json_object * json_object_array_get_idx(struct json_object * json_array,int i);

说明:

从数组中,按下标取JSON值对象。

参数:

json_array – json 数组类型对象

i – 数组下标位置

定义宏 json_object_object_foreach(obj,key,val)

说明:

遍历json对象的key和值 (key, val默认参数不变)

3.代码示例

#include
#include
#include
#include

#include"json.h"

int main(int argc, char **aggv)
{
        struct json_tokener *tok;
        struct json_object *my_string, *my_int, *my_object, *my_array;
        struct json_object *new_obj;
        int i;
        int bar = 13; 
    
        my_string = json_object_new_string("\t");
        printf("my_string = %s\n", json_object_get_string(my_string));
        printf("my_string.to_string()=%s\n",json_object_to_json_string(my_string));
        json_object_put(my_string);

        my_string = json_object_new_string("\\");
        printf("my_string = %s\n", json_object_get_string(my_string));
        printf("my_string.to_string()=%s\n",json_object_to_json_string(my_string));
        json_object_put(my_string);
    
        my_string = json_object_new_string("foo");
        printf("my_string = %s\n", json_object_get_string(my_string));
        printf("my_string.to_string()=%s\n",json_object_to_json_string(my_string));
        json_object_put(my_string);

    
        my_int = json_object_new_int(9);
        printf("my_int = %d\n", json_object_get_int(my_int));
        printf("my_int.to_string()=%s\n",json_object_to_json_string(my_int));

        my_array = json_object_new_array();
    
        json_object_array_add(my_array,json_object_new_int(1));
        json_object_array_add(my_array,json_object_new_int(2));
        json_object_array_add(my_array,json_object_new_int(3));
        json_object_array_put_idx(my_array,4,json_object_new_int(5));
        printf("my_array=\n");

        for(i = 0; i < json_object_array_length(my_array); i++) {
                struct json_object *obj = json_object_array_get_idx(my_array, i);
                printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
        }
        printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));

        my_object = json_object_new_object();

        json_object_object_add(my_object, "adc", json_object_new_int(12));
        json_object_object_add(my_object, "foo", json_object_new_int(bar));
        json_object_object_add(my_object, "bool0", json_object_new_boolean(0));
        json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
        json_object_object_add(my_object, "baz", json_object_new_string("bang"));
//      json_object_object_del(my_object,"baz");


        printf("my_object=\n");
        json_object_object_foreach(my_object, key, val) {
                printf("\t%s:%s\n", key, json_object_to_json_string(val));
        }
        printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));

        new_obj=json_tokener_parse("\"\003\"");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("/*hello*/\"foo\"");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("//hello\n\"foo\"");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("\"\\u0041\\u0042\\u0043\"");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("null");
         printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("Ture");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("12");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("12.3");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("[\"\\n\"]");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("[\"\\nabc\\n\"]");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("[null]");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("[]");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("[false]");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("[\"abc\",null,\"def\",12]");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("{}");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("{\"foo\":\"bar\"}");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("{\"foo\":\"bar\",\"baz\":null,\"bool0\":ture}");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("{\"foo\":[null,\"foo\"]}");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("{\"abc\":12,\"foo\":\"bar\",\"bool0\":false,\"bool1\":ture,\"arr\":[1,2,3,null,5]}");
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);

        new_obj=json_tokener_parse("{ foo }");
//      if(is_error(new_obj))printf("got error as expected\n");

        new_obj=json_tokener_parse("foo");
//      if(is_error(new_obj))printf("got error as expected\n");

        new_obj=json_tokener_parse("{\"foo");
//      if(is_error(new_obj))printf("got error as expected\n");

        tok = json_tokener_new();
        new_obj = json_tokener_parse_ex(tok, "{\"foo", 6);
//      if(is_error(new_obj))printf("got error as expected\n");
        new_obj = json_tokener_parse_ex(tok, "\":{\"bar", 8);
//      if(is_error(new_obj))printf("got error as expected\n");
        new_obj = json_tokener_parse_ex(tok, "\":13}}", 6);
        printf("new_obj_.to.string()=%s\n",json_object_to_json_string(new_obj));
        json_object_put(new_obj);
        json_tokener_free(tok);
        json_object_put(my_string);
        json_object_put(my_int);
        json_object_put(my_object);
        json_object_put(my_array);

        return 0;
}


由于编译时需要的连接库还有头文件路径,所以写成Makefile方便一点
Makefile:

CC=gcc
INC=/usr/local/include/json-c/
LIB=json-c
LIB_PATH=/usr/local/lib/

json:json.c
        $(CC) -o $@ $< -I$(INC) -L$(LIB_PATH) -l$(LIB)

.PHONY clean:
        -rm -f json

结果:

my_string = 	
my_string.to_string()="\t"
my_string = \
my_string.to_string()="\\"
my_string = foo
my_string.to_string()="foo"
my_int = 9
my_int.to_string()=9
my_array=
	[0]=1
	[1]=2
	[2]=3
	[3]=null
	[4]=5
my_array.to_string()=[ 1, 2, 3, null, 5 ]
my_object=
	adc:12
	foo:13
	bool0:false
	bool1:true
	baz:"bang"
my_object.to_string()={ "adc": 12, "foo": 13, "bool0": false, "bool1": true, "baz": "bang" }
new_obj_.to.string()="\u0003"
new_obj_.to.string()="foo"
new_obj_.to.string()="foo"
new_obj_.to.string()="ABC"
new_obj_.to.string()=null
new_obj_.to.string()=null
new_obj_.to.string()=12
new_obj_.to.string()=12.3
kayshi@ubuntu:~/code/json$ ./json 
my_string = 	
my_string.to_string()="\t"
my_string = \
my_string.to_string()="\\"
my_string = foo
my_string.to_string()="foo"
my_int = 9
my_int.to_string()=9
my_array=
	[0]=1
	[1]=2
	[2]=3
	[3]=null
	[4]=5
my_array.to_string()=[ 1, 2, 3, null, 5 ]
my_object=
	adc:12
	foo:13
	bool0:false
	bool1:true
	baz:"bang"
my_object.to_string()={ "adc": 12, "foo": 13, "bool0": false, "bool1": true, "baz": "bang" }
new_obj_.to.string()="\u0003"
new_obj_.to.string()="foo"
new_obj_.to.string()="foo"
new_obj_.to.string()="ABC"
new_obj_.to.string()=null
new_obj_.to.string()=null
new_obj_.to.string()=12
new_obj_.to.string()=12.3
new_obj_.to.string()=[ "\n" ]
new_obj_.to.string()=[ "\nabc\n" ]
new_obj_.to.string()=[ null ]
new_obj_.to.string()=[ ]
new_obj_.to.string()=[ false ]
new_obj_.to.string()=[ "abc", null, "def", 12 ]
new_obj_.to.string()={ }
new_obj_.to.string()={ "foo": "bar" }
new_obj_.to.string()=null
new_obj_.to.string()={ "foo": [ null, "foo" ] }
new_obj_.to.string()=null
new_obj_.to.string()={ "foo": { "bar": 13 } }

你可能感兴趣的:(JSON)