“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、null、对象(object)或者数组(array)。这些结构可以嵌套>>
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string),字符串(string)与C或者Java的字符串非常相似。
数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式,除去一些编码细节。
1、json结构体初始化
struct json_object* json_object_new_object()
json_object *newobj = NULL;????
2、向json结构体中添加key-value对
void json_object_object_add( struct json_object* dest, const char *key, struct json_object *value )
3、返回json结构体的字符串格式
const char* json_object_to_json_string(struct json_object *obj)
4、基本数据类型转换为json结构体
struct json_object* json_object_new_string(const char *s)
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_boolean(json_bool b)
struct json_object* json_object_new_double(double d)
5、json结构体打印
json_object_to_file( jsonfile , json_res ); //打印到文件
printf(“%s”, json_object_to_json_string(json_res) ); //打印到标准输出
6、 将一个json 文件变成object对象:
struct json_object* json_object_from_file(char *filename)
举例:
json_object *pobj = NULL;
pobj = json_object_from_file(“/home/boy/tmp/test/jsontest/test.json”);
7、将json-object写回文件
int json_object_to_file(char *filename, struct json_object *obj)
举例:
json_object_to_file(“test.json”, pobj);
8、删除一个对象
void json_object_object_del(struct json_object* jso, const char *key);
9、增加一个对象
void json_object_object_add(struct json_object* jso, const char *key, struct json_object *val);
举例:
json_object_object_add(pobj, “Name”, json_object_new_string(“Andy”));
json_object_object_add(pobj, “Age”, json_object_new_int(200));
10、释放对象
void json_object_put(struct json_object *jso);
(1): 新建一个x.json文件
{
“item1”: “I love JSON”,
“foo”: “You love Json”,
“item3”: “We love JSON”,
“Name”: “Andy”,
“Age”: 28
}
(2): 程序
#include
#include
#include
#include
#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:
break;
}
}
}
int main(void)
{
json_object *pobj = NULL;
//pobj = json_tokener_parse("{ \"abc\": 12, \"foo\": \"bar\", \"bool0\": false, \"bool1\": true, \"arr\":
//[ 1, 2, 3, null, 5 ] }");
//解析字符串生成,json对象
//printf("new_obj.to_string()=%s\n", json_object_to_json_string(pobj));
//json_parse(pobj);
pobj = json_object_from_file("/home/boy/tmp/test/jsontest/test.json");
GetValByKey(pobj, "foo");
json_object_object_del(pobj, "foo");
json_object_object_add(pobj, "foo", json_object_new_string("fark"));
json_object_object_add(pobj, "Age", json_object_new_int(200));
GetValByKey(pobj, "Age");
json_object_to_file("/home/boy/tmp/test/jsontest/new.json", pobj);
json_object_put(pobj);
return 0;
}
from:
http://blog.sina.com.cn/s/blog_63041bb80101bq24.html
http://kuafu80.blog.163.com/blog/static/12264718020118182915988/
(3) json-c 写配置文件比较简单,并且解析配置文件也比较省事。配置文件:
#include
#include
#include
#include
#include "json.h"
#define CONFIG_FILE "config.json"
typedef struct{
int len3;
int len4;
}A_A_s;
typedef struct {
char name[100];
int len;
A_A_s A1_1;
}A_s;
typedef struct {
int B_len;
int B_len3;
}B_s;
typedef struct {
int C_len;
int C_len3;
}C_s;
typedef struct {
int root1;
int root2;
char token[100];
A_s A1;
B_s B1;
C_s C1;
}root_s;
int main(int argc, char **argv)
{
struct json_object *new_obj;
struct json_object *root_obj;
MC_SET_DEBUG(1);
root_s config_struct =
{
.root1 = 1,
.root2 = 2,
.token = "token1",
.A1 =
{
.name = "A_name_1",
.len = 101,
.A1_1 =
{ .len3 = 1001,
.len4 = 1002,
},
},
.B1 =
{
.B_len = 201,
.B_len3 = 202,
},
.C1 =
{
.C_len = 301,
.C_len3 = 302,
},
};
root_obj = json_object_new_object();
new_obj = json_object_new_int(config_struct.root1);
json_object_object_add(root_obj,"L1_root1",new_obj);
new_obj = json_object_new_int(config_struct.root2);
json_object_object_add(root_obj,"L1_root2",new_obj);
new_obj = json_object_new_string(config_struct.token);
json_object_object_add(root_obj,"L1_root_token",new_obj);
json_object *json_obj_A,*json_obj_B,*json_obj_C,*json_obj_A_A1;
json_obj_A = json_object_new_object();
json_obj_B = json_object_new_object();
json_obj_C = json_object_new_object();
json_obj_A_A1 = json_object_new_object();
json_object_object_add(json_obj_A_A1,"A_A1_len3",json_object_new_int(config_struct.A1.A1_1.len3));
json_object_object_add(json_obj_A_A1,"A_A1_len4",json_object_new_int(config_struct.A1.A1_1.len4));
json_object_object_add(json_obj_A,"A_name",json_object_new_string(config_struct.A1.name));
json_object_object_add(json_obj_A,"A_len",json_object_new_int(config_struct.A1.len));
json_object_object_add(json_obj_A,"A_A1_Struct",json_obj_A_A1);
json_object_object_add(json_obj_B,"B_len",json_object_new_int(config_struct.B1.B_len));
json_object_object_add(json_obj_B,"B_len3",json_object_new_int(config_struct.B1.B_len3));
json_object_object_add(json_obj_C,"C_len",json_object_new_int(config_struct.C1.C_len));
json_object_object_add(json_obj_C,"C_len3",json_object_new_int(config_struct.C1.C_len3));
json_object_object_add(root_obj,"L1_A1",json_obj_A);
json_object_object_add(root_obj,"L1_B1",json_obj_B);
json_object_object_add(root_obj,"L1_C1",json_obj_C);
json_object_to_file(CONFIG_FILE,root_obj);
json_object_put(root_obj);
json_object_put(json_obj_A);
json_object_put(json_obj_A_A1);
json_object_put(json_obj_B);
json_object_put(json_obj_C);
json_object_put(new_obj);
return 0;
}
写完的配置文件如下:
{
“L1_root1”: 1,
“L1_root2”: 2,
“L1_root_token”: “token1”,
“L1_A1”: {
“A_name”: “A_name_1”,
“A_len”: 101,
“A_A1_Struct”: {
“A_A1_len3”: 1001,
“A_A1_len4”: 1002
}
},
“L1_B1”: {
“B_len”: 201,
“B_len3”: 202
},
“L1_C1”: {
“C_len”: 301,
“C_len3”: 302
}
}
json_object_object_add 该动作如果要添加的Key已经存在,则修改之,否则才会添加。所以想要修改配置文件,直接用add的方法就好了,不要用del+add的方式修改。