#include "WZYyjson.h"
#include "yyjson.h"
//#include
#include
#include
using namespace std;
WZYyjson::WZYyjson()
{
}
WZYyjson::~WZYyjson()
{
}
void read_json() {
const char *json = "{\"name\":\"Mash\",\"star\":4,\"hits\":[2,2,1,3]}";
yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *obj_name = yyjson_obj_get(root, "name");
const char* name = yyjson_get_str(obj_name);
//char *name2 = yyjson_get_str(obj_name); //这个不行,因为yyjson_get_str()返回的是个const char*
printf("my name:%s\n",name);
yyjson_val *obj_star = yyjson_obj_get(root, "star");
int star = yyjson_get_int(obj_star);
printf("my star:%d\n",star);
yyjson_val *obj_hits = yyjson_obj_get(root, "hits");
int length = yyjson_arr_size(obj_hits);
printf("hits arr length= %d\n",length);
size_t idx, max;
yyjson_val *obj_hit;
yyjson_arr_foreach(obj_hits, idx, max, obj_hit) {
printf("idx :%d max:%d value:%d\n",idx,max,yyjson_get_int(obj_hit));
}
yyjson_doc_free(doc);
// Read JSON and get root
/*yyjson_doc *doc = yyjson_read(json, strlen(json), 0);
yyjson_val *root = yyjson_doc_get_root(doc);*/
// Get root["name"]
//yyjson_val *name = yyjson_obj_get(root, "name");
//printf("name: %s\n", yyjson_get_str(name));
//printf("name length:%d\n", (int)yyjson_get_len(name));
Get root["star"]
//yyjson_val *star = yyjson_obj_get(root, "star");
//printf("star: %d\n", (int)yyjson_get_int(star));
Get root["hits"], iterate over the array
//yyjson_val *hits = yyjson_obj_get(root, "hits");
//size_t idx, max;
//yyjson_val *hit;
//yyjson_arr_foreach(hits, idx, max, hit) {
// printf("hit%d: %d\n", (int)idx, (int)yyjson_get_int(hit));
//}
Free the doc
//yyjson_doc_free(doc);
vector
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
/*int size = vec1.size();
for (int i = 0; i
}*/
for (int i = 0; i < vec1.size(); i++)
{
cout << vec1[i] << endl<<"";
}
char bytes[10];
for (int i = 0; i < 10; i++) {
//bytes[i] = 48+i;
bytes[i] = 0 + i;
}
printf("bytes:%s\n",bytes);
char *name2 = "hello";
printf("name:%s\n", name2);
char nam3[6];
nam3[0] = 'h';
nam3[1] = 'e';
nam3[2] = 'l';
nam3[3] = 'l';
nam3[4] = 'h';
nam3[5] = '\0';
printf("nam3:%s\n", nam3);
char a = 1;
printf("a:%d\n", a);
printf("a:%c\n", a);
}
void read_json2() {
//1、首先获取doc
yyjson_doc *doc = yyjson_read_file("test.json", 0, NULL, NULL);
if (doc == NULL) {
printf("doc is NULL\n");
return;
}
else {
printf("doc is not NULL\n");
}
int count = yyjson_doc_get_val_count(doc);
printf("count :%d\n", count); //几个数据6,整个数组再算一个,几个(3)名字算上是10个
int read_size = yyjson_doc_get_read_size(doc);
printf("read_size:%d\n", read_size);
yyjson_val *yyjson_doc_name = yyjson_doc_get_pointer(doc, "name");
const char *name1 = yyjson_get_str(yyjson_doc_name);
printf("name1 :%s\n",name1); //按理说能获取但是为什么获取不了呢
//2、再获取跟-root
yyjson_val *root = yyjson_doc_get_root(doc);
if (root == NULL) {
printf("root is NULL\n");
return;
}
else {
printf("root is not NULL\n");
}
//通过root 获取"name"对应数据,必须先获取obj对象,然后依据内容使用对应的yyjson_get_str yyjson_get_int获取数据
yyjson_val * yyjson_name = yyjson_obj_get(root, "name");
const char *name = yyjson_get_str(yyjson_name);
printf("name :%s\n", name);
int len = yyjson_get_len(yyjson_name);//Returns the content length (string length in bytes, array size,object size
printf("len:%d\n", len);
yyjson_val *obj_age = yyjson_obj_get(root, "age");
int age = yyjson_get_int(obj_age);
printf("age :%d\n", age);
len = yyjson_get_len(obj_age); //整个因为不是字符串 数组,所以返回0
printf("len:%d\n", len);
/*yyjson_val *obj_name = yyjson_obj_get(root, "name");
const char* name = yyjson_get_str(obj_name);*/
yyjson_val *obj_score = yyjson_obj_get(root, "score");
len = yyjson_get_len(obj_score);
printf("obj_score len:%d\n", len);
yyjson_val *arr_score_idx = yyjson_arr_get(obj_score, 0);
int score = yyjson_get_int(arr_score_idx);
printf("score:%d", score);
double score_f = yyjson_get_real(arr_score_idx); //不能用yyjson_get_real接收int类型
printf("score_f:%lf", score_f);
yyjson_type type = yyjson_get_type(arr_score_idx);
printf("type :%d\n", type);
const char *type_desc = yyjson_get_type_desc(arr_score_idx);
printf("type_desc :%s\n", type_desc);
printf("--------------\n");
arr_score_idx = yyjson_arr_get(obj_score, 1);
score_f = yyjson_get_real(arr_score_idx);
printf("score_f:%lf", score_f);
type = yyjson_get_type(arr_score_idx);
printf("type :%d\n", type);
type_desc = yyjson_get_type_desc(arr_score_idx);
printf("type_desc :%s\n", type_desc);
printf("--------------\n");
arr_score_idx = yyjson_arr_get(obj_score, 3);
type = yyjson_get_type(arr_score_idx); //获取数据类型
printf("type :%d\n", type);
type_desc = yyjson_get_type_desc(arr_score_idx);
printf("type_desc :%s\n", type_desc);
bool flag = yyjson_get_bool(arr_score_idx);
Teacher//
yyjson_val *teacher = yyjson_obj_get(root, "teacher");
yyjson_val *obj_t_name = yyjson_obj_get(teacher, "name");//需要的是父节点,这里不在是root节点了
const char *t_name = yyjson_get_str(obj_t_name);
printf("t_name:%s\n",t_name);
yyjson_val *arr_student = yyjson_obj_get(teacher, "student");
len = yyjson_get_len(arr_student);
printf("arr_student size:%d\n",len);
/使用迭代器获取数据
yyjson_val * val;
/*yyjson_arr_iter *iter;
yyjson_arr_iter_init(arr_student, iter);*/
yyjson_arr_iter iter;
yyjson_arr_iter_init(arr_student, &iter);
while ((val = yyjson_arr_iter_next(&iter)))
{
yyjson_val *obj_student_name = yyjson_obj_get(val, "name");
const char *stu_name = yyjson_get_str(obj_student_name);
yyjson_val *obj_student_grade = yyjson_obj_get(val, "grade");
int stu_grade = yyjson_get_int(obj_student_grade);
printf("stu_name:%s stu_grade :%d\n", stu_name, stu_grade);
/*const char *stu_grade = yyjson_get_str(obj_student_grade); int 只能使用int 不能使用str
sprintf("stu_name:%s stu_grade :%s\n", stu_name, stu_grade);*/
}
//使用foreach
yyjson_val *item;
size_t idx, max;
yyjson_arr_foreach(arr_student, idx, max, item) {
yyjson_val *obj_student_name = yyjson_obj_get(item, "name");
const char *stu_name = yyjson_get_str(obj_student_name);
yyjson_val *obj_student_grade = yyjson_obj_get(item, "grade");
int stu_grade = yyjson_get_int(obj_student_grade);
printf("stu_nameaaa:%s stu_gradebbb :%d\n", stu_name, stu_grade);
printf("idx:%d max:%d", idx, max);
}
//最后一定要释放doc
yyjson_doc_free(doc);
}
void write_json() {
yyjson_doc *read_doc = yyjson_read_file("test.json", 0, NULL, NULL);
yyjson_mut_doc *mut_doc = yyjson_doc_mut_copy(read_doc, NULL);
if (mut_doc == NULL) {
printf("doc is NULL\n");
return;
}
else {
printf("doc is not NULL\n");
//yyjson_doc_free(read_doc);
}
//读现成的数据/
yyjson_mut_val * root = yyjson_mut_doc_get_root(mut_doc);
yyjson_mut_val *obj_name = yyjson_mut_obj_get(root,"name");
const char * name = yyjson_mut_get_str(obj_name);
printf("aaaaaname %s \n", name);
/给root新增一个key :value
yyjson_mut_val *key = yyjson_mut_str(mut_doc, "page");
yyjson_mut_val *num = yyjson_mut_int(mut_doc, 123);
yyjson_mut_obj_add(root, key, num);
yyjson_mut_val *obj_teacher = yyjson_mut_obj_get(root, "teacher");
yyjson_mut_val *obj_t_name = yyjson_mut_obj_get(obj_teacher, "name");
const char *t_name = yyjson_mut_get_str(obj_t_name);
printf("t_name:%s\n", t_name);
//给teacher增加项 男朋友项
yyjson_mut_val *key_boyfriend = yyjson_mut_str(mut_doc, "boyfriend");
/*yyjson_mut_val *val_boyfriend = yyjson_mut_str(mut_doc, "张继科");*/ //这里没有支持中文
yyjson_mut_val *val_boyfriend = yyjson_mut_str(mut_doc, "123");
bool flag = yyjson_mut_obj_add(obj_teacher, key_boyfriend, val_boyfriend);
//清除teacher的age
//yyjson_mut_val *obj_age = yyjson_mut_obj_get(obj_teacher, "age");
//flag = yyjson_mut_obj_remove(obj_teacher, obj_age); //这个
//flag = yyjson_mut_obj_clear(obj_teacher);//这个是把teacher中的内容删除了,但是teacher还在
yyjson_mut_val *obj_age = yyjson_mut_str(mut_doc, "age"); // 删除teacher的age,不能通过teacher获得age,需要类似于new一个ageyyjson_mut_str(mut_doc, "age");才能删除
flag = yyjson_mut_obj_remove(obj_teacher, obj_age);
//给teacher的student增加一项
//yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc);
yyjson_mut_val *arr_student = yyjson_mut_obj_get(obj_teacher, "student");
yyjson_mut_val *newStudent = yyjson_mut_obj(mut_doc);//新增一个student对象
yyjson_mut_val *key_stu_name = yyjson_mut_str(mut_doc, "name"); //key
yyjson_mut_val *val_stu_name = yyjson_mut_str(mut_doc, "stu10"); //value
yyjson_mut_val *key_stu_grade = yyjson_mut_str(mut_doc, "grade"); //grade key
yyjson_mut_val *val_stu_grade = yyjson_mut_int(mut_doc, 100); //grade val
yyjson_mut_obj_add(newStudent, key_stu_name, val_stu_name); //把key value 添加到newStuendt
yyjson_mut_obj_add(newStudent, key_stu_grade, val_stu_grade);//把grade key value 添加到newStudent
yyjson_mut_arr_append(arr_student, newStudent);//把新的newStudnet 添加到student最后面
//给teacher的student删除第二项
//yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, size_t idx);
yyjson_mut_arr_remove(arr_student, 1);
//yyjson_mut_doc_set_root(mut_doc, root);
const char *json = yyjson_mut_write(mut_doc, 0, NULL);
printf("json:%s\n",json);
释放
yyjson_mut_doc_free(mut_doc);
yyjson_doc_free(read_doc);
//最后需要把json写入文件
FILE* fp = fopen("test.json", "w");
if (NULL != fp)
{
fwrite(json, strlen(json), 1, fp); /* 保存到文件 */
// fputs(buff,fp);
fclose(fp); /* 关闭文件指针 */
}
}
void do_config_json() {
/*yyjson_doc *read_doc = yyjson_read_file("test.json", 0, NULL, NULL);
yyjson_mut_doc *mut_doc = yyjson_doc_mut_copy(read_doc, NULL);*/
yyjson_doc *read_doc = yyjson_read_file("config.json", 0, NULL, NULL);
yyjson_mut_doc *mut_doc = yyjson_doc_mut_copy(read_doc, NULL);
if (read_doc == NULL) {
printf("doc is NULL\n");
return;
}
yyjson_mut_val * root = yyjson_mut_doc_get_root(mut_doc);
yyjson_mut_val * obj_device_info = yyjson_mut_obj_get(root, "device_info");
yyjson_mut_val * arr_net_ipv4 = yyjson_mut_obj_get(obj_device_info, "net_ipv4");
int len = yyjson_mut_get_len(arr_net_ipv4);
//printf("len=%d\n", len);
bool flag = yyjson_mut_obj_remove(obj_device_info, arr_net_ipv4);
//yyjson_mut_val * arr_net_ipv42 = yyjson_mut_str(mut_doc, "net_ipv4"); //删除net_ipv4第一种方法
//flag = yyjson_mut_obj_remove(obj_device_info, arr_net_ipv42);
//yyjson_mut_arr_remove(arr_net_ipv4, 0); //删除方法2,指定索引删除
flag = yyjson_mut_arr_clear(arr_net_ipv4); //删除方法3,清除索引数组数据,只留net_ipv4名字[] "net_ipv4":[]
yyjson_mut_val *new_eth0 = yyjson_mut_obj(mut_doc);//新增一个student对象
yyjson_mut_val *net_name_key = yyjson_mut_str(mut_doc, "net_name");
yyjson_mut_val *net_name_val = yyjson_mut_str(mut_doc, "eth2");
yyjson_mut_obj_add(new_eth0, net_name_key, net_name_val);
yyjson_mut_val *ip_addr_key = yyjson_mut_str(mut_doc, "ip_addr");
yyjson_mut_val *ip_addr_val = yyjson_mut_str(mut_doc, "192.168.100.55");
yyjson_mut_obj_add(new_eth0, ip_addr_key, ip_addr_val);
yyjson_mut_val *ip_port_key = yyjson_mut_str(mut_doc, "ip_port");
yyjson_mut_val *ip_port_val = yyjson_mut_int(mut_doc, 502);
yyjson_mut_obj_add(new_eth0, ip_port_key, ip_port_val);
yyjson_mut_val *ip_mask_key = yyjson_mut_str(mut_doc, "ip_mask");
yyjson_mut_val *ip_mask_val = yyjson_mut_str(mut_doc, "255.0.0.0");
yyjson_mut_obj_add(new_eth0, ip_mask_key, ip_mask_val);
yyjson_mut_val *gateway_key = yyjson_mut_str(mut_doc, "gateway");
yyjson_mut_val *gateway_val = yyjson_mut_str(mut_doc, "192.168.100.1");
yyjson_mut_obj_add(new_eth0, gateway_key, gateway_val);
yyjson_mut_val *net_dhcp_key = yyjson_mut_str(mut_doc, "net_dhcp");
yyjson_mut_val *net_dhcp_val = yyjson_mut_int(mut_doc, 0);
yyjson_mut_obj_add(new_eth0, net_dhcp_key, net_dhcp_val);
yyjson_mut_val *lrrq_key = yyjson_mut_str(mut_doc, "lrrq");
yyjson_mut_val *lrrq_val = yyjson_mut_str(mut_doc, "1689150480");
yyjson_mut_obj_add(new_eth0, lrrq_key, lrrq_val);
yyjson_mut_arr_append(arr_net_ipv4, new_eth0);
/*yyjson_mut_val * obj_name = yyjson_mut_obj_get(obj_device_info, "device_name");
const char * device_name = yyjson_mut_get_str(obj_name);
flag = yyjson_mut_obj_remove(obj_device_info, obj_name);*/ //这个直接删除不掉 ,需要new 一个 新的(mut_doc 而不是 obj_device_info的) device_name 再删除(ojb_device_info的)才行
//yyjson_mut_val *obj_name2 = yyjson_mut_str(mut_doc, "device_name"); // 删除teacher的age,不能通过teacher获得age,需要类似于new一个age yyjson_mut_str(mut_doc, "age");才能删除
//flag = yyjson_mut_obj_remove(obj_device_info, obj_name2);
//len = yyjson_get_len(arr_student);
const char *json = yyjson_mut_write(mut_doc, 0, NULL);
printf("%s\n",json);
yyjson_mut_doc_free(mut_doc);
yyjson_doc_free(read_doc);
FILE* fp = fopen("config.json", "w");
if (NULL != fp)
{
fwrite(json, strlen(json), 1, fp); /* 保存到文件 */
// fputs(buff,fp);
fclose(fp); /* 关闭文件指针 */
}
}
int main() {
//read_json();
//read_json2();
//write_json();
do_config_json();
printf("aaaabbb\n");
system("pause");
}