#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main()
{
char *json_string = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
// 解析JSON字符串
cJSON *cjson = cJSON_Parse(json_string);
if (cjson == NULL) {
printf("Error before: [%s]\n", cJSON_GetErrorPtr());
return 1;
}
// 获取元素的值
cJSON *name = cJSON_GetObjectItem(cjson, "name");
cJSON *age = cJSON_GetObjectItem(cjson, "age");
cJSON *city = cJSON_GetObjectItem(cjson, "city");
// 打印元素的值
printf("name: %s\n", cJSON_GetStringValue(name));
printf("age: %d\n", cJSON_GetNumberValue(age));
printf("city: %s\n", cJSON_GetStringValue(city));
// 释放cJSON对象的内存
cJSON_Delete(cjson);
return 0;
}
cjson解析json
最新推荐文章于 2024-07-21 21:11:33 发布