简介
- 本文章主要介绍下parson库的使用
- parson : parson是用C语言开发的一个json解析库, 提供json的序列化和反序列化功能。使用时只需要在我们的工程中引入头文件parson.h和源文件parson.c即可使用。
使用parson解析json
-
单层json结构解析
- 代码
-
const char* resp = "{\"code\":0, \"flag\":true, \"msg\":\"success\"}"; bool bRet = false; JSON_Value *rootValue = NULL; do { //反序列化为json对象 rootValue = json_parse_string(resp); if (rootValue == NULL) { break; } JSON_Object *rootObject = json_value_get_object(rootValue); if (rootObject == NULL) { break; } if (!json_object_has_value(rootObject, "code")) { break; } int code = json_object_get_number(rootObject, "code"); if (!json_object_has_value(rootObject, "flag")) { break; } bool flag = json_object_get_boolean(rootObject, "flag"); if (!json_object_has_value(rootObject, "msg")) { break; } const char* msg = json_object_get_string(rootObject, "msg"); printf("code = %d, flag = %d, msg = %s\n", code, flag, msg); bRet = true; } while (0); if (rootValue != NULL) { json_value_free(rootValue); rootValue = NULL; } return bRet;
- 执行结果
-
code = 0, flag = 1, msg = success
-
多层json结构解析
- 代码
-
const char* resp = "{\"code\":0, \"msg\":\"success\", \"data\":{\"name\":\"lucy\", \"score\":99.5}}"; bool bRet = false; JSON_Value *rootValue = NULL; do { rootValue = json_parse_string(resp); if (rootValue == NULL) { break; } JSON_Object *rootObject = json_value_get_object(rootValue); if (rootObject == NULL) { break; } if (!json_object_has_value(rootObject, "code")) { break; } int code = json_object_get_number(rootObject, "code"); if (!json_object_has_value(rootObject, "msg")) { break; } const char* msg = json_object_get_string(rootObject, "msg"); if (!json_object_has_value(rootObject, "data")) { break; } JSON_Object *dataObject = json_object_get_object(rootObject, "data"); if (dataObject == NULL) { break; } if (!json_object_has_value(dataObject, "name")) { break; } const char* name = json_object_get_string(dataObject, "name"); if (!json_object_has_value(dataObject, "score")) { break; } double score = json_object_get_number(dataObject, "score"); printf("code = %d, msg = %s, name = %s, score = %lf\n", code, msg, name, score); bRet = true; } while (0); if (rootValue != NULL) { json_value_free(rootValue); rootValue = NULL; } return bRet;
- 执行结果
-
code = 0, msg = success, name = lucy, score = 99.500000
-
有数组的json结构解析
- 代码
-
const char* resp = "{\"code\":0, \"msg\":\"success\", \"result\":[\"tcp\", \"udp\", \"http\", \"tls\"]}"; bool bRet = false; JSON_Value *root_value = NULL; do { root_value = json_parse_string(resp); if (root_value == NULL) { break; } JSON_Object *root_object = json_value_get_object(root_value); if (root_object == NULL) { break; } if (!json_object_has_value(root_object, "code")) { break; } int code = json_object_get_number(root_object, "code"); printf("code is %d\n", code); if (!json_object_has_value(root_object, "msg")) { break; } const char* msg = json_object_get_string(root_object, "msg"); printf("msg is %s\n", msg); if (!json_object_has_value(root_object, "result")) { break; } JSON_Array *result_obj = json_object_get_array(root_object, "result"); if (result_obj == NULL) { break; } printf("result: "); for (int i = 0; i < json_array_get_count(result_obj); i++) { const char *pStr = json_array_get_string(result_obj, i); if (pStr != NULL) { printf("%s ", pStr); } } printf("\n"); bRet = true; } while (0); if (root_value != NULL) { json_value_free(root_value); root_value = NULL; } return bRet;
- 执行结果
-
code is 0 msg is success result: tcp udp http tls
-
json文件解析
- 文件内容 test.json
-
{"code":0,"flag":true,"msg":"success"}
- 代码
-
bool bRet = false; JSON_Value *rootValue = NULL; do { // 如果json文件内容带注释可用这个函数 json_parse_file_with_comments rootValue = json_parse_file("test.json"); if (rootValue == NULL) { break; } JSON_Object *rootObject = json_value_get_object(rootValue); if (rootObject == NULL) { break; } if (!json_object_has_value(rootObject, "code")) { break; } int code = json_object_get_number(rootObject, "code"); if (!json_object_has_value(rootObject, "flag")) { break; } bool flag = json_object_get_boolean(rootObject, "flag"); if (!json_object_has_value(rootObject, "msg")) { break; } const char* msg = json_object_get_string(rootObject, "msg"); printf("code = %d, flag = %d, msg = %s\n", code, flag, msg); bRet = true; } while (0); if (rootValue != NULL) { json_value_free(rootValue); rootValue = NULL; } return bRet;
- 执行结果
-
code = 0, flag = 1, msg = success
使用parson构建json
-
单层json结构构建
- 代码
-
bool bRet = false; JSON_Value *root_value = NULL; do { root_value = json_value_init_object(); if (root_value == NULL) { break; } JSON_Object *root_object = json_value_get_object(root_value); if (root_object == NULL) { break; } json_object_set_number(root_object, "code", 0); json_object_set_boolean(root_object, "flag", true); json_object_set_string(root_object, "msg", "success"); json_object_set_number(root_object, "rate", 10.5); char *data = json_serialize_to_string(root_value); if (data == NULL) { break; } printf("%s\n", data); bRet = true; json_free_serialized_string(data); } while (0); if (root_value != NULL) { json_value_free(root_value); root_value = NULL; } return bRet;
- 执行结果
-
{"code":0,"flag":true,"msg":"success","rate":10.5}
-
多层json结构构建
- 代码
-
bool bRet = false; JSON_Value *root_value = NULL; do { root_value = json_value_init_object(); if (root_value == NULL) { break; } JSON_Object *root_object = json_value_get_object(root_value); if (root_object == NULL) { break; } JSON_Value *data_value = json_value_init_object(); if (data_value == NULL) { break; } JSON_Object *data_object = json_value_get_object(data_value); if (data_object == NULL) { break; } json_object_set_string(data_object, "name", "jack"); json_object_set_number(data_object, "age", 28); json_object_set_number(root_object, "code", 0); json_object_set_boolean(root_object, "flag", true); json_object_set_string(root_object, "msg", "success"); json_object_set_value(root_object, "data", data_value); char *data = json_serialize_to_string(root_value); if (data == NULL) { break; } printf("%s\n", data); bRet = true; json_free_serialized_string(data); } while (0); if (root_value != NULL) { json_value_free(root_value); root_value = NULL; } return bRet;
- 执行结果
-
{"code":0,"flag":true,"msg":"success","data":{"name":"jack","age":28}}
-
带有数组的json结构构建
- 代码
-
bool bRet = false; JSON_Value *root_value = NULL; do { root_value = json_value_init_object(); if (root_value == NULL) { break; } JSON_Object *root_object = json_value_get_object(root_value); if (root_object == NULL) { break; } JSON_Value *data_value = json_value_init_object(); if (data_value == NULL) { break; } JSON_Object *data_object = json_value_get_object(data_value); if (data_object == NULL) { break; } JSON_Value *dataValueArray = json_value_init_array(); if (dataValueArray == NULL) { break; } JSON_Array *dataObjectArray = json_value_get_array(dataValueArray); if (dataObjectArray == NULL) { break; } for (int i = 0; i < 5; i++) { JSON_Value *pValue = json_value_init_object(); if (pValue == NULL) { break; } JSON_Object *pObject = json_value_get_object(pValue); if (pObject == NULL) { break; } json_object_set_number(pObject, "index", i); json_object_set_string(pObject, "status","success"); json_array_append_value(dataObjectArray, pValue); } json_object_set_string(data_object, "name", "jack"); json_object_set_number(data_object, "age", 28); json_object_set_number(root_object, "code", 0); json_object_set_boolean(root_object, "flag", true); json_object_set_string(root_object, "msg", "success"); json_object_set_value(root_object, "data", dataValueArray); char *data = json_serialize_to_string(root_value); if (data == NULL) { break; } printf("%s\n", data); bRet = true; json_free_serialized_string(data); } while (0); if (root_value != NULL) { json_value_free(root_value); root_value = NULL; } return bRet;
- 执行结果
-
{"code":0,"flag":true,"msg":"success","data":[{"index":0,"status":"success"},{"index":1,"status":"success"},{"index":2,"status":"success"},{"index":3,"status":"success"},{"index":4,"status":"success"}]}
-
构建json并保存到文件
- 代码
-
bool bRet = false; JSON_Value *root_value = NULL; do { root_value = json_value_init_object(); if (root_value == NULL) { break; } JSON_Object *root_object = json_value_get_object(root_value); if (root_object == NULL) { break; } json_object_set_number(root_object, "code", 0); json_object_set_boolean(root_object, "flag", true); json_object_set_string(root_object, "msg", "success"); json_object_set_number(root_object, "rate", 10.5); JSON_Status status = json_serialize_to_file(root_value, "test.json"); if (status == JSONFailure) { break; } bRet = true; } while (0); if (root_value != NULL) { json_value_free(root_value); root_value = NULL; } return bRet;
- test.json文件内容
-
{"code":0,"flag":true,"msg":"success","rate":10.5}
文章来源地址https://www.toymoban.com/news/detail-457180.html
文章来源:https://www.toymoban.com/news/detail-457180.html
到了这里,关于c语言json库parson的介绍和使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!