网络传输中,数据交互也需要一些自定义协议,其实json就是最好用之一。
使用cjson进行json字符串的构造,以及对其进行对应解析的测试代码。
1:背景介绍
JSNON作为一种轻量级的数据交互格式,易于阅读和编写,易于解析和生成。
日常开发中对json的使用次数也越来越多。
我常用的就是使用cjon以及jsoncpp对数据进行构造以及解析。
cjson因为直接包含一个头文件和.c文件就可以使用,特别方便,这里整理一些使用cjson进行构造与解析的实例,做代码备份。
2:测试代码
2.1:构造json格式的数据,返回json字符串
char * make_json_str()
{
cJSON *root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("test"));
cJSON_AddItemToObject(root, "age", cJSON_CreateNumber(80));
cJSON *languages = cJSON_CreateArray();
cJSON_AddItemToArray(languages, cJSON_CreateString("C++"));
cJSON_AddItemToArray(languages, cJSON_CreateString("Java"));
cJSON_AddItemToObject(root, "languages", languages);
cJSON *phone = cJSON_CreateObject();
cJSON_AddItemToObject(phone, "number", cJSON_CreateString("1867216i144"));
cJSON_AddItemToObject(phone, "type", cJSON_CreateString("home"));
cJSON_AddItemToObject(root, "phone", phone);
cJSON *book0 = cJSON_CreateObject();
cJSON_AddItemToObject(book0, "name", cJSON_CreateString("Linux kernel development"));
cJSON_AddItemToObject(book0, "price", cJSON_CreateNumber(7.7));
cJSON *book1 = cJSON_CreateObject();
cJSON_AddItemToObject(book1, "name", cJSON_CreateString("Linux server development"));
cJSON_AddItemToObject(book1, "price", cJSON_CreateNumber(8.0));
cJSON *books = cJSON_CreateArray();
cJSON_AddItemToArray(books, book0);
cJSON_AddItemToArray(books, book1);
cJSON_AddItemToObject(root, "books", books);
cJSON_AddItemToObject(root, "vip", cJSON_CreateBool(1));
cJSON_AddItemToObject(root, "address", cJSON_CreateString(""));
char *jsonstr = cJSON_Print(root);
cJSON_Delete(root);
printf("make json str is [%lu][%s] \n", strlen(jsonstr), jsonstr);
return jsonstr;
}
2.2:json字符串对应的解析
void printCJson(cJSON *root);
int parse_json_str(const char * jsonstr)
{
if(jsonstr == NULL)
{
printf("parse json error: jsonstr is null \n");
return -1;
}
cJSON *root = cJSON_Parse(jsonstr);
if(root == NULL)
{
printf("parse json error: cJSON_Parse error \n");
return -1;
}
printCJson(root);
cJSON_Delete(root);
return 0;
}
void printCJson(cJSON *root)
{
cJSON *name = cJSON_GetObjectItem(root, "name");
cJSON *age = cJSON_GetObjectItem(root, "age");
cJSON *languages = cJSON_GetObjectItem(root, "languages");
cJSON *phone = cJSON_GetObjectItem(root, "phone");
cJSON *books = cJSON_GetObjectItem(root, "books");
cJSON *vip = cJSON_GetObjectItem(root, "vip");
cJSON *address = cJSON_GetObjectItem(root, "address");
printf(" name: %s\n", name->valuestring);
printf(" age: %d\n", age->valueint);
printf(" languages: ");
for (int i = 0; i < cJSON_GetArraySize(languages); i++)
{
cJSON *lang = cJSON_GetArrayItem(languages, i);
if (i != 0)
{
printf(", ");
}
printf("%s", lang->valuestring);
}
printf("\n");
cJSON *number = cJSON_GetObjectItem(phone, "number");
cJSON *type = cJSON_GetObjectItem(phone, "type");
printf(" phone number: %s, type: %s\n", number->valuestring, type->valuestring);
for (int i = 0; i < cJSON_GetArraySize(books); i++)
{
cJSON *book = cJSON_GetArrayItem(books, i);
cJSON *name = cJSON_GetObjectItem(book, "name");
cJSON *price = cJSON_GetObjectItem(book, "price");
printf(" book name: %s, price: %lf\n", name->valuestring, price->valuedouble);
}
printf(" vip: %d\n", vip->valueint);
if (address && !cJSON_IsNull(address))
{
printf(" address: %s\n", address->valuestring);
}
else
{
printf(" address is null\n");
}
}
2.3:测试调用主函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
int main()
{
char * strjson = make_json_str();
parse_json_str(strjson);
free(strjson);
return 0;
}
3:执行结果
make json str is [286][{
"name": "test",
"age": 80,
"languages": ["C++", "Java"],
"phone": {
"number": "1867216i144",
"type": "home"
},
"books": [{
"name": "Linux kernel development",
"price": 7.7
}, {
"name": "Linux server development",
"price": 8
}],
"vip": true,
"address": ""
}]
name: test
age: 80
languages: C++, Java
phone number: 1867216i144, type: home
book name: Linux kernel development, price: 7.700000
book name: Linux server development, price: 8.000000
vip: 1
address:
我开始试着积累一些常用代码:自己代码库中备用
我的知识储备更多来自这里,推荐你了解:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习
|