了解json-c library
更新記錄
item | note |
---|---|
20160909 | 第一版 |
目錄
JSON
JSON (JavaScript Object Notation)
Introducing JSON
![[json] [json]](/2016/09/09/json-c/json-c.png)
- object
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace).
Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
由大括號 { }包起來
由逗號(,)分開每一組
每一組使用(key-value)方式儲存 (即 name-value)
![[object] [object]](/2016/09/09/json-c/object.gif)
array
An array is an ordered collection of values.
An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
由中括號 { }包起來value
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array
These structures can be nested.
![[value] [value]](/2016/09/09/json-c/value.gif)
json object的鍵值(key),一定要用文字做鍵值
json-c example
json-c
json_object.h
json_object_object_add - Add an object field to a json_object of type json_type_object
json_object_new_int - Create a new empty json_object of type json_type_int
json_object_get_string_len - Get the string length of a json_object1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17* @param obj the json_object instance
* @param key the object field name (a private copy will be duplicated)
* @param val a json_object or NULL member to associate with the given field
extern void json_object_object_add(struct json_object* obj, const char *key,
struct json_object *val);
struct json_object* json_object_new_int(int32_t i);
struct json_object* json_object_new_int64(int64_t i);
struct json_object* json_object_new_double(double d);
struct json_object* json_object_new_string(const char *s);
struct json_object* json_object_new_string_len(const char *s, int len);
int32_t json_object_get_int(struct json_object *obj);
int64_t json_object_get_int64(struct json_object *obj);
double json_object_get_double(struct json_object *obj);
const char* json_object_get_string(struct json_object *obj);
int json_object_get_string_len(struct json_object *obj);type
1
2
3
4
5
6
7
8
9
10typedef enum json_type {
/* If you change this, be sure to update json_type_to_name() too */
json_type_null,
json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string,
} json_type;
json_object_put
- json_object_put
主要清除之前產生的記億体,防止memory leak1
2
3
4
5
6
7
8
9
10
11
12
13
14
15int json_object_put(struct json_object *jso)
{
if(jso)
{
jso->_ref_count--;
if(!jso->_ref_count)
{
if (jso->_user_delete)
jso->_user_delete(jso, jso->_userdata);
jso->_delete(jso);
return 1;
}
}
return 0;
}
example
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46$ cat main.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "json.h"
void getValByKey(json_object* jobj, const char *sname)
{
json_object *pval =NULL;
enum json_type type;
pval = json_object_object_get(jobj, sname);
if(NULL != pval){
type = json_object_get_type(pval);
switch(type)
{
case json_type_string:
printf("key:%s, value:%s\n",sname,json_object_get_string(pval));
break;
case json_type_int:
printf("key:%s, value:%d\n",sname,json_object_get_int(pval));
break;
default:
printf("default: key:%s\n",sname);
break;
}
}
}
int main(void)
{
json_object *pobj = NULL;
pobj = json_object_from_file("/root/t1.json");
getValByKey(pobj,"green");
json_object_object_del(pobj,"red");
json_object_object_add(pobj,"black",json_object_new_string("#000"));
json_object_object_add(pobj,"iblack",json_object_new_int(0));
getValByKey(pobj,"iblack");
json_object_to_file("/tmp/t1-new.json",pobj);
json_object_put(pobj);
return 0;
}test
1
2
3
4
5
6
7
8
9
10
11
12
13gk350a :[~]# ./json-t1
key:green, value:#0f0
key:iblack, value:0
gk350a :[~]# cat /tmp/t1-new.json
gk350a :[~]# cat /tmp/t1-new.json
{"green":"#0f0","blue":"#00f","black":"#000","iblack":0}gk350a :[~]#
gk350a :[~]# cat t1.json
{
"red":"#f00",
"green":"#0f0",
"blue":"#00f",
}
gk350a :[~]#