c語言開發JSON

安裝json-c 庫json

$ sudo apt-get install libjson0-dev libjson0

代碼json_test.c數組

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include<json/json.h>

int main(int argc, char **argv)
{
	struct json_tokener *tok;
	struct json_object *my_string, *my_int, *my_object, *my_array;
	struct json_object *new_obj;
	int i;

	my_string = json_object_new_string("\t");

	/*輸出 my_string=   */
	printf("my_string=%s\n", json_object_get_string(my_string));

	/*轉換json格式字符串 輸出my_string.to_string()="\t"*/
	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));

	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));

	/*建立個空json對象值數組類型*/
	my_array = json_object_new_array();

	/*添加json值類型到數組中*/
	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名稱和值到json對象集合中*/
	json_object_object_add(my_object, "abc",json_object_new_int(12));
	json_object_object_add(my_object, "foo",json_object_new_string("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"));

	/*一樣的key 添加會替換掉*/
	json_object_object_add(my_object, "baz",json_object_new_string("fark"));
	json_object_object_del(my_object, "baz");
	printf("my_object=\n");

	/*遍歷json對象集合*/
	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("True");
	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");

	/* test incremental parsing */
	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;
}

運行:測試

$ gcc -o json_test json_test.c -ljson
$ ./json_test 
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=
	abc: 12
	foo: "bar"
	bool0: false
	bool1: true
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }
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()=true
new_obj.to_string()=12
got error as expected
got error as expected
new_obj.to_string()={ "foo": { "bar": 13 } }
相關文章
相關標籤/搜索