cJSON使用案例代码。
#include <stdio.h>
#include <stdlib.h>
#include "../cJson/cJSON.h"
int main() {
FILE* jsonFile = fopen("data.json", "r");
if (jsonFile) {
fseek(jsonFile, 0, SEEK_END);
int jsonFileSize = ftell(jsonFile);
fseek(jsonFile, 0, SEEK_SET);
char* jsonStr = (char*)malloc(jsonFileSize + 2);
fread(jsonStr, jsonFileSize, 1, jsonFile);
fclose(jsonFile);
cJSON* root;
root = cJSON_Parse(jsonStr);
if (root) {
cJSON* typeObj01 = cJSON_GetObjectItem(root, "type");
if (typeObj01) {
printf("type:%s\r\n", typeObj01->valuestring);
}
cJSON* crsObj = cJSON_GetObjectItem(root, "crs");
if (c