一、内核链表
了解内核链表
Linux内核中的链表
linux内核中的链表 - wangLinuxer - 博客园
练习
将下面的数据节点信息转换为链表结构,并遍历输出。要求根据type的值来决定val的类型。type为1代表bool类型,2代表整形,3代表浮点型。无需解析文本,直接赋值形成节点即可。
"data":
[
{
"key": 1,
"type": 2,
"val": "10"
},
{
"key": 2,
"type": 1,
"val": "0"
},
{
"key": 3,
"type": 3,
"val": "22.5"
}
]
注意:key
、type
和val
都需要保存到链表节点中去,val上述例子描述是字符串,但是实际存储的时候需要根据type的类型进行变化(这里不用判断,直接按照逻辑赋值即可)。遍历输出时候,也要根据相应的类型进行变化输出(遍历过程中需要判断,然后根据类型进行相应的输出)。
listCore.c
#include <stdio.h>
#include "list.h" //list.h中包含内核链表的定义
typedef int BOOL;
union val
{
BOOL val_b;
int val_i;
float val_f;
};
struct data
{
int key;
int type;
union val val_t;
};
struct list_node
{
struct data data_t;
struct list_head list;
};
int main(int argc, char const *argv[])
{
struct list_head head;
INIT_LIST_HEAD(&head);
struct list_node data1;
data1.data_t.key = 1;
data1.data_t.type = 2;
data1.data_t.val_t.val_i = 10;
struct list_node data2;
data2.data_t.key = 2;
data2.data_t.type = 1;
data2.data_t.val_t.val_b = 0;
struct list_node data3;
data3.data_t.key = 3;
data3.data_t.type = 3;
data3.data_t.val_t.val_f = 22.5;
list_add(&data1.list,&head);
list_add(&data2.list,&head);
list_add(&data3.list,&head);
struct list_head *pos;
struct list_node *tmp;
list_for_each(pos,&head)
{
tmp = list_entry(pos,struct list_node,list);
if(1 == tmp->data_t.type)
{
printf("%d %d %d\n",tmp->data_t.key,tmp->data_t.type,\
tmp->data_t.val_t.val_b);
}
else if(2 == tmp->data_t.type)
{
printf("%d %d %d\n",tmp->data_t.key,tmp->data_t.type,\
tmp->data_t.val_t.val_i);
}
else if(3 == tmp->data_t.type)
{
printf("%d %d %f\n",tmp->data_t.key,tmp->data_t.type,\
tmp->data_t.val_t.val_f);
}
}
return 0;
}
二、JSON
了解json
了解Json基本的概念
JSON基本操作
Json序列化:利用程序生成Json字符串的过程。
Json反序列化:利用程序将已有的Json字符串解析出我们需要的值的过程。
使用cJson
cJSON使用详解
Json与CJson详解_RoosPan的博客-CSDN博客_cjson和json
练习
根据以上示例,将下面内容存为文件,读取后解析。
{
"ver": "1.0",
"cloud": {
"password": "12345678",
"mpassword": "12345678"
},
"data": [{
"key": 1,
"type": 2,
"val": "10"
},
{
"key": 2,
"type": 1,
"val": "0"
},
{
"key": 3,
"type": 3,
"val": "22.5"
}
]
}
json_out.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "cJSON.h"
/
//使用cJSON库对下面的JSON文件进行反序列化操作。
//ver和cloud的节点直接打印即可,data的数据节点解析后形成链表存储。
/
typedef int BOOL;
union json_val
{
BOOL val_b;
int val_i;
float val_f;
};
struct json_data
{
int key;
int type;
union json_val json_val_t;
};
struct list_node
{
struct json_data json_data_t;
struct list_head list;
};
int main(int argc, char const *argv[])
{
char out[1024] = {0};
FILE *fp = fopen("./cJSON.json","r");
size_t size = fread(out,sizeof(out),1,fp);//获取文件大小:seek,stat
if(size < 0)
{
perror("fread error.");
return -1;
}
// printf("%s\n",out);
cJSON *json = cJSON_Parse(out);
if(!json)
{
printf("get out faild.\n");
return -1;
}
cJSON *js_ver = cJSON_GetObjectItem(json,"ver");
if(!js_ver)
{
printf("no ver.\n");
return -1;
}
printf("ver:%s\n",js_ver->valuestring);
cJSON *js_cloud = cJSON_GetObjectItem(json,"cloud");
if(!js_cloud)
{
printf("no cloud.\n");
return -1;
}
cJSON *password = cJSON_GetObjectItem(js_cloud,"password");
if(!password)
{
printf("no password.\n");
return -1;
}
cJSON *mpassword = cJSON_GetObjectItem(js_cloud,"mpassword");
if(!mpassword)
{
printf("no mpassword.\n");
return -1;
}
printf("cloud_password:%s\n",password->valuestring);
printf("cloud_mpassword:%s\n",mpassword->valuestring);
struct list_head head;
INIT_LIST_HEAD(&head);
cJSON *js_data = cJSON_GetObjectItem(json,"data");
if(!js_data)
{
printf("no data.\n");
return -1;
}
int array_size = cJSON_GetArraySize(js_data);
int i = 0;
cJSON *item;
for(i=0;i<array_size;i++)
{
struct list_node *node = (struct list_node *)malloc(sizeof(struct list_node));
cJSON *item = cJSON_GetArrayItem(js_data,i);
cJSON *item_key = cJSON_GetObjectItem(item,"key");
cJSON *item_type = cJSON_GetObjectItem(item,"type");
cJSON *item_val = cJSON_GetObjectItem(item,"val");
node->json_data_t.key = item_key->valueint;
node->json_data_t.type = item_type->valueint;
if(1 == node->json_data_t.type)
{
node->json_data_t.json_val_t.val_b = atoi(item_val->valuestring);
}
else if(2 == node->json_data_t.type)
{
node->json_data_t.json_val_t.val_i = atoi(item_val->valuestring);
}
else if(3 == node->json_data_t.type)
{
node->json_data_t.json_val_t.val_f = atof(item_val->valuestring);
}
list_add(&node->list,&head);
// printf("item:%d %d %s\n",item_key->valueint,item_type->valueint,item_val->valuestring);
}
struct list_head *pos;
struct list_node *tmp;
list_for_each_prev(pos,&head)
{
tmp = list_entry(pos,struct list_node,list);
if(1 == tmp->json_data_t.type)
{
printf("%d %d %d\n",tmp->json_data_t.key,tmp->json_data_t.type,\
tmp->json_data_t.json_val_t.val_b);
}
if(2 == tmp->json_data_t.type)
{
printf("%d %d %d\n",tmp->json_data_t.key,tmp->json_data_t.type,\
tmp->json_data_t.json_val_t.val_i);
}
if(3 == tmp->json_data_t.type)
{
printf("%d %d %.1f\n",tmp->json_data_t.key,tmp->json_data_t.type,\
tmp->json_data_t.json_val_t.val_f);
}
}
//解析完成后,再利用解析完的变量和链表,序列化为json字符串直接打印即可
cJSON *js_new = cJSON_CreateObject();
cJSON_AddStringToObject(js_new,"ver","1.0");
cJSON *obj = NULL;
cJSON_AddItemToObject(js_new,"cloud",obj=cJSON_CreateObject());
cJSON_AddStringToObject(obj,"password","12345678");
cJSON_AddStringToObject(obj,"mpassword","12345678");
// cJSON_AddItemToObject(obj,"mpassword",cJSON_CreateString("12345678"));
cJSON *array = NULL;
cJSON_AddItemToObject(js_new,"data",array=cJSON_CreateArray());
list_for_each_prev(pos, &head)
{
cJSON *array_obj = NULL;
char buf[64]={0};
tmp = list_entry(pos,struct list_node,list);
cJSON_AddItemToArray(array,array_obj=cJSON_CreateObject());
cJSON_AddNumberToObject(array_obj,"key",tmp->json_data_t.key);
cJSON_AddNumberToObject(array_obj,"type",tmp->json_data_t.type);
if(1 == tmp->json_data_t.type)
{
sprintf(buf,"%d",tmp->json_data_t.json_val_t.val_b);
}
else if(2 == tmp->json_data_t.type)
{
sprintf(buf,"%d",tmp->json_data_t.json_val_t.val_i);
}
else if(3 == tmp->json_data_t.type)
{
sprintf(buf,"%.1f",tmp->json_data_t.json_val_t.val_f);
}
cJSON_AddStringToObject(array_obj,"val",buf);
}
char *new_out = cJSON_Print(js_new);
printf("%s\n",new_out);
fclose(fp);
cJSON_Delete(json);
return 0;
}
结果