cJson也是JSON的一个C库,官网在这
https://github.com/DaveGamble/cJSON
主要版本有
Version | Date |
---|---|
1.7.15 | 2021-08-25 |
1.7.14 | 2020-09-03 |
1.7.13 | 2020-04-02 |
1.7.0 | 2017-12-31 |
1.6.0 | 2017-10-09 |
1.5.0 | 2017-05-02 |
1.4.0 | 2017-03-04 |
1.3.0 | 2017-02-17 |
1.2.0 | 2017-01-09 |
1.1.0 | 2016-12-06 |
1.0.0 | 2016-11-17 |
与Jansson功能差不多,挑几个API对比一下
cJSON | Jansson |
---|---|
cJSON_Parse | json_loads |
cJSON_load_from_file | json_load_file |
cJSON_PrintUnformatted | json_dumps |
cJSON_dump_to_file | json_dump_file |
cJSON_GetArraySize | json_object_sizejson_array_size |
cJSON_IsObject | json_is_object |
cJSON_IsArray | json_is_array |
cJSON_IsString | json_is_string |
cJSON_IsNumber | json_is_number |
cJSON_IsTrue | json_is_true |
cJSON_IsFalse | json_is_false |
cJSON_IsBool | json_is_boolean |
cJSON_IsNull | json_is_null |
cJSON_GetObjectItem | json_object_get |
cJSON_GetArrayItem | json_array_get |
cJSON_Delete | json_delete |
Vx7里集成了基于1.7.14移植的1.7.14.1,移植到Vx6也是很容易的
写个小栗子
代码语言:javascript复制/*
* 版权所有 公众号 VxWorks567
*/
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
void testCjson(char *jsonfile)
{
cJSON *root;
cJSON *object;
char *value;
if(jsonfile == NULL)
root = cJSON_Parse("{"name1": "value1", "name2": "value2", "name3": "value3", "name4": "value4"}");
else
root = cJSON_load_from_file(jsonfile);
if(root == NULL)
{
printf("cJSON_Parse fail![%s]n", cJSON_GetErrorPtr());
return;
}
value = cJSON_PrintUnformatted(root);
printf("%sn", value);
free(value);
value = cJSON_Print(root);
printf("%sn", value);
free(value);
printf("object count %dn", cJSON_GetArraySize(root));
object = cJSON_GetObjectItem(root, "name1");
if(object != NULL)
{
value = cJSON_Print(object);
printf("First: %s->%sn", object->string, value);
free(value);
}
cJSON_Delete(root);
return;
}