我不知道如何“简化”json 会有用。第一次在 c 中使用 json 可能会很吓人。
我喜欢cJSON
图书馆,它轻巧、便携且稳定。它具有良好的测试覆盖率,并且许可证是 MIT。
我认为使用该库的这段代码可以满足cJSON
您的要求:
#include <cjson/cJSON.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <float.h>
const char json1[] = "{\"field1\":\"something\",\"whatever\":10,\"description\":\"body\",\"id\":\"random\",\"__oh__\":{\"session\":\"12345678jhgfdrtyui\"}}";
const char json2[] = "{\"status\":1,\"query\":{},\"proc\":{\"memory\":{\"total\":17177939968,\"cmax\":18363625472,\"amax\":20000000000},\"cpu\":{\"cores\":[0.788,0.132,0.319,2.951,10.111,3.309,1.43,0.8,2.705,4.203,2.32,2,0.019,0.172,0.247,3.888,0.282,0.423,5.254,0.258,0.009,0.369,3.277,0.048,0.283,7.574,3.086,1.592,0.191,0.166,4.348,0.391,0.085,0.25,7.12,4.927,3.671,1.147,3.216,4.628,0.131,0.995,0.744,4.252,4.022,3.505,3.758,3.491],\"total\":108.886,\"limit\":800},\"disk\":{\"used\":20170,\"limit\":50000,\"io_limit\":500}}}";
const char json3[] = "{\"Name\":\"Tom\",\"Age\":18,\"Address\":\"California\",\"arr\":[1,2,3,4,5]}";
static void simplifyArray(cJSON *input, cJSON *output)
{
cJSON *item;
size_t noElems = 0;
if (cJSON_IsString(cJSON_GetArrayItem(input, 0))) {
size_t max, min;
max = 0;
min = UINT_MAX;
cJSON_ArrayForEach(item, input) {
noElems++;
size_t len = strlen(cJSON_GetStringValue(item));
if (len > max) max = len;
if (len < min) min = len;
}
cJSON *newArray = cJSON_AddArrayToObject(output, input->string);
cJSON_AddItemToArray(newArray, cJSON_CreateNumber(noElems));
cJSON_AddItemToArray(newArray, cJSON_CreateNumber(max));
cJSON_AddItemToArray(newArray, cJSON_CreateNumber(min));
} else if (cJSON_IsNumber(cJSON_GetArrayItem(input, 0))) {
double max, min;
max = -DBL_MAX;
min = DBL_MAX;
cJSON_ArrayForEach(item, input) {
noElems++;
double value = item->valuedouble;
if (value > max) max = value;
if (value < min) min = value;
}
cJSON *newArray = cJSON_AddArrayToObject(output, input->string);
cJSON_AddItemToArray(newArray, cJSON_CreateNumber(noElems));
cJSON_AddItemToArray(newArray, cJSON_CreateNumber(max));
cJSON_AddItemToArray(newArray, cJSON_CreateNumber(min));
}
}
static void simplify(cJSON *input, cJSON *output)
{
cJSON *elem;
for (elem = input; elem != NULL; elem = elem->next) {
if (cJSON_IsString(elem)) {
cJSON_AddNumberToObject(output, elem->string, strlen(cJSON_GetStringValue(elem)));
} else if (cJSON_IsArray(elem)) {
simplifyArray(elem, output);
} else if (cJSON_IsObject(elem)) {
cJSON *newOutput = cJSON_AddObjectToObject(output, elem->string);
simplify(elem->child, newOutput);
} else {
cJSON *dup = cJSON_Duplicate(elem, true);
cJSON_AddItemToObject(output, elem->string, dup);
}
}
}
static void simplifyAndPrint(const char *json)
{
cJSON *input = cJSON_Parse(json);
cJSON *output = cJSON_CreateObject();
simplify(input->child, output);
printf("%s\n", cJSON_PrintUnformatted(output));
cJSON_Delete(input);
cJSON_Delete(output);
}
int main()
{
simplifyAndPrint(json1);
simplifyAndPrint(json2);
simplifyAndPrint(json3);
return 0;
}
输出:
{"field1":9,"whatever":10,"description":4,"id":6,"__oh__":{"session":18}}
{"status":1,"query":{},"proc":{"memory":{"total":17177939968,"cmax":18363625472,"amax":20000000000},"cpu":{"cores":[48,10.111,0.009],"total":108.886,"limit":800},"disk":{"used":20170,"limit":50000,"io_limit":500}}}
{"Name":3,"Age":18,"Address":10,"arr":[5,5,1]}
在上面的示例中,我希望不要更改输入 JSON,如果您不关心这一点,您可以使用该函数cJSON_ReplaceItemInObject
来替换节点。
PS:我假设数组只包含字符串和数字,不要混用,因为没有规则可以处理其他数组配置。
PS2:此代码使用的是 Ubuntu 20.04 中存在的库版本,如果您从 GitHub 下载该库,该版本将包含更多功能。