c语言:给定如下json文件,如何使用cjson统计成员birtharea的分布?
[
{
"wyId":275580,
"shortName": "Cesar Domingo",
"firstName": "C\u00e9sar Domingo",
"middleName": "",
"lastName": "Mendiondo L\u00f3pez",
"birthDate": null,
"birthArea": {
"id": 76,
"alpha2code": "BR",
"alpha3code": "BRA",
"name": "Brazil"
},
"passportArea": {
"id": 76,
"alpha2code": "BR",
"alpha3code": "BRA",
"name": "Brazil"
},
"currentTeamId": 0
},
{
"wyId": 14710,
"shortName": "J. Heynckes",
"firstName": "Josef",
"middleName": "",
"lastName": "Heynckes",
"birthDate": "1945-05-09",
"birthArea": {
"id": 276,
"alpha2code": "DE",
"alpha3code": "DEU",
"name": "Germany"
},
"passportArea": {
"id": 276,
"alpha2code": "DE",
"alpha3code": "DEU",
"name": "Germany"
},
"currentTeamId": 0
},
{
"wyId": 135480,
"shortName": "G. De Biasi",
"firstName": "Giovanni",
"middleName": "",
"lastName": "De Biasi",
"birthDate": "1956-06-16",
"birthArea": {
"id": 380,
"alpha2code": "IT",
"alpha3code": "ITA",
"name": "Italy"
},
"passportArea": {
"id": 380,
"alpha2code": "IT",
"alpha3code": "ITA",
"name": "Italy"
},
"currentTeamId": 0
},
{
"wyId": 210074,
"shortName": "Marcelino",
"firstName": "Marcelino",
"middleName": "",
"lastName": "Garc\u00eda Toral",
"birthDate": "1965-08-14",
"birthArea": {
"id": 724,
"alpha2code": "ES",
"alpha3code": "ESP",
"name": "Spain"
},
"passportArea": {
"id": 724,
"alpha2code": "ES",
"alpha3code": "ESP",
"name": "Spain"
},
"currentTeamId": 674
},
{
"wyId": 293398,
"shortName": "T. Korkut",
"firstName": "Tayfun",
"middleName": "",
"lastName": "Korkut",
"birthDate": "1974-04-02",
"birthArea": {
"id": 276,
"alpha2code": "DE",
"alpha3code": "DEU",
"name": "Germany"
},
"passportArea": {
"id": 792,
"alpha2code": "TR",
"alpha3code": "TUR",
"name": "Turkey"
},
"currentTeamId": 0
},
{
"wyId": 92894,
"shortName": "Ernesto Valverde",
"firstName": "Ernesto",
"middleName": "",
"lastName": "Valverde Tejedor",
"birthDate": "1964-02-09",
"birthArea": {
"id": 724,
"alpha2code": "ES",
"alpha3code": "ESP",
"name": "Spain"
},
"passportArea": {
"id": 724,
"alpha2code": "ES",
"alpha3code": "ESP",
"name": "Spain"
},
"currentTeamId": 676
},
{
"wyId": 3880,
"shortName": "Unzu\u00e9",
"firstName": "Juan Carlos",
"middleName": "",
"lastName": "Unzu\u00e9 Labiano",
"birthDate": "1967-04-22",
"birthArea": {
"id": 724,
"alpha2code": "ES",
"alpha3code": "ESP",
"name": "Spain"
},
"passportArea": {
"id": 724,
"alpha2code": "ES",
"alpha3code": "ESP",
"name": "Spain"
},
"currentTeamId": 0
},
{
"wyId": 17121,
"shortName": "D. Schuster",
"firstName": "Dirk",
"middleName": "",
"lastName": "Schuster",
"birthDate": "1967-12-29",
"birthArea": {
"id": 276,
"alpha2code": "DE",
"alpha3code": "DEU",
"name": "Germany"
},
"passportArea": {
"id": 276,
"alpha2code": "DE",
"alpha3code": "DEU",
"name": "Germany"
},
"currentTeamId": 0
},
{
"wyId": 20454,
"shortName": "A. Conte",
"firstName": "Antonio",
"middleName": "",
"lastName": "Conte",
"birthDate": "1969-07-31",
"birthArea": {
"id": 380,
"alpha2code": "IT",
"alpha3code": "ITA",
"name": "Italy"
},
"passportArea": {
"id": 380,
"alpha2code": "IT",
"alpha3code": "ITA",
"name": "Italy"
},
"currentTeamId": 0
},
]
json和cjson这块,自学了很久也搞不懂,就比较糟心。
楼主把自己遇到的例子放在这里,希望能给一些具体的代码作为参考(最好附带详细一点的解析),感激不尽
希望输出的是成员birtharea数据中name的分布,比如有多少位成员是Brazil,有多少位成员是germany这样的,json文件名就是member.json
你的member.json文件中最后一个大括号后面不应该有逗号
然后下面只实现打印出成员来自哪里,未做统计
重点在于解析,请参考
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main(void)
{
FILE *fp = fopen("./member.json", "r");
if (fp == NULL) {
printf("open member.json failed!\n");
return -1;
}
fseek(fp, 0, SEEK_END);
int total = ftell(fp);
char *value = (char *)calloc(total+1, sizeof(char));
if (value == NULL) {
printf("calloc memory failed!\n");
return -1;
}
fseek(fp, 0, SEEK_SET);
fread(value, sizeof(char), total, fp);
fclose(fp);
// 上面是读取member.json文件,然后这里开始解析
// 使用cJSON_Parse函数解析成json结构体
// json结构体中包括next,prev,child三个字段,next和prev表示同级,child表示该json结构的子结构
// 在member.json文件中,有一个根json结构,它是一个数组。也就是[]
// 数组中每个结构都是同级的,所以在下面for循环中通过next获取
// 在每个结构中,通过cJSON_GetObjectItem函数获取某个名称对应值(值类型又区分json对象、数组、字符串、数字等)
cJSON *object = cJSON_Parse(value);
if (object == NULL) {
printf("parse member.json file failed!\n");
return -1;
}
cJSON *p = object;
while (p != NULL) {
cJSON *child = p->child;
for (cJSON *q = child; q != NULL; q = q->next) {
cJSON *birtharea = cJSON_GetObjectItem(q, "birthArea"); // 获取birthArea结构
if (birtharea == NULL) {
continue;
}
cJSON *name = cJSON_GetObjectItem(birtharea, "name"); // 获取birthArea结构下name结构
if (name == NULL) {
continue;
}
printf("come from %s\n", cJSON_GetStringValue(name)); // 获取name对应的值,即字符串
}
p = p->next;
}
free(value);
return 0;
}