参考网址:
(65条消息) cJSON使用详细教程 | 一个轻量级C语言JSON解析器_Mculover666的博客(嵌入式_)
cJSON-github
生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <stdio.h> #include "cJSON.h"
int main(void) { cJSON* cjson_test = NULL; cJSON* cjson_address = NULL; cJSON* cjson_skill = NULL; char* str = NULL;
cjson_test = cJSON_CreateObject();
cJSON_AddStringToObject(cjson_test, "name", "mculover666");
cJSON_AddNumberToObject(cjson_test, "age", 22);
cJSON_AddNumberToObject(cjson_test, "weight", 55.5);
cjson_address = cJSON_CreateObject(); cJSON_AddStringToObject(cjson_address, "country", "China"); cJSON_AddNumberToObject(cjson_address, "zip-code", 111111); cJSON_AddItemToObject(cjson_test, "address", cjson_address);
cjson_skill = cJSON_CreateArray(); cJSON_AddItemToArray(cjson_skill, cJSON_CreateString( "C" )); cJSON_AddItemToArray(cjson_skill, cJSON_CreateString( "Java" )); cJSON_AddItemToArray(cjson_skill, cJSON_CreateString( "Python" )); cJSON_AddItemToObject(cjson_test, "skill", cjson_skill);
cJSON_AddFalseToObject(cjson_test, "student");
str = cJSON_Print(cjson_test); printf("%s\n", str);
return 0; }
|
解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <stdio.h> #include "cJSON.h"
char *message = "{ \ \"name\":\"mculover666\", \ \"age\": 22, \ \"weight\": 55.5, \ \"address\": \ { \ \"country\": \"China\",\ \"zip-code\": 111111\ }, \ \"skill\": [\"c\", \"Java\", \"Python\"],\ \"student\": false \ }";
int main(void) { cJSON* cjson_test = NULL; cJSON* cjson_name = NULL; cJSON* cjson_age = NULL; cJSON* cjson_weight = NULL; cJSON* cjson_address = NULL; cJSON* cjson_address_country = NULL; cJSON* cjson_address_zipcode = NULL; cJSON* cjson_skill = NULL; cJSON* cjson_student = NULL; int skill_array_size = 0, i = 0; cJSON* cjson_skill_item = NULL;
cjson_test = cJSON_Parse(message); if(cjson_test == NULL) { printf("parse fail.\n"); return -1; }
cjson_name = cJSON_GetObjectItem(cjson_test, "name"); cjson_age = cJSON_GetObjectItem(cjson_test, "age"); cjson_weight = cJSON_GetObjectItem(cjson_test, "weight");
printf("name: %s\n", cjson_name->valuestring); printf("age:%d\n", cjson_age->valueint); printf("weight:%.1f\n", cjson_weight->valuedouble);
cjson_address = cJSON_GetObjectItem(cjson_test, "address"); cjson_address_country = cJSON_GetObjectItem(cjson_address, "country"); cjson_address_zipcode = cJSON_GetObjectItem(cjson_address, "zip-code"); printf("address-country:%s\naddress-zipcode:%d\n", cjson_address_country->valuestring, cjson_address_zipcode->valueint);
cjson_skill = cJSON_GetObjectItem(cjson_test, "skill"); skill_array_size = cJSON_GetArraySize(cjson_skill); printf("skill:["); for(i = 0; i < skill_array_size; i++) { cjson_skill_item = cJSON_GetArrayItem(cjson_skill, i); printf("%s,", cjson_skill_item->valuestring); } printf("\b]\n");
cjson_student = cJSON_GetObjectItem(cjson_test, "student"); if(cjson_student->valueint == 0) { printf("student: false\n"); } else { printf("student:error\n"); } return 0; }
|
相关 - JSMN(仅解析)
JSMN介绍和使用_心飞的博客-CSDN博客
The most simple JSON parser in C for small systems (zserge.com)
zserge/jsmn: Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket (github.com)