#include #include "c_cross_pack/libs/string.h" #include "cJSON/cJSON.c" int main() { FILE* fp = fopen("data.json", "r"); char buffer[2048]; fread(buffer, 1, sizeof(buffer), fp); fclose(fp); // Parse the JSON data cJSON *json = cJSON_Parse(buffer); // Variables for working with parsing json int count = cJSON_GetArraySize(json); cJSON *name, *age, *student, *grades, *math, *physics, *history; // What we are looking for int average_best, aver_best_i; int stud_80_plus[10]; int studs_80_i = 0; average_best = -1; // Find some data int average; for (int i = 0; i < count; i++) { student = cJSON_GetArrayItem(json, i); grades = cJSON_GetObjectItem(student, "grades"); math = cJSON_GetObjectItem(grades, "math"); physics = cJSON_GetObjectItem(grades, "physics"); history = cJSON_GetObjectItem(grades, "history"); average = (math->valueint + physics->valueint + history->valueint) / 3; if (average > average_best) { average_best = average; aver_best_i = i; } if (math->valueint > 80) { stud_80_plus[studs_80_i] = i; studs_80_i++; } } // Format average best string aver_best_str = strInit(""); // Get data student = cJSON_GetArrayItem(json, aver_best_i); name = cJSON_GetObjectItem(student, "name"); age = cJSON_GetObjectItem(student, "age"); grades = cJSON_GetObjectItem(student, "grades"); math = cJSON_GetObjectItem(grades, "math"); physics = cJSON_GetObjectItem(grades, "physics"); history = cJSON_GetObjectItem(grades, "history"); // Add strAdd(&aver_best_str, name->valuestring); strAdd(&aver_best_str, " "); strAdd(&aver_best_str, int2Str(&age->valueint)); strAdd(&aver_best_str, " "); strAdd(&aver_best_str, int2Str(&math->valueint)); strAdd(& aver_best_str, "-"); strAdd(&aver_best_str, int2Str(&physics->valueint)); strAdd(&aver_best_str, "-"); strAdd(&aver_best_str, int2Str(&history->valueint)); // Format math 80+ string stud_80_str = strInit(""); for (int i = 0; i < studs_80_i; i++) { // Get data student = cJSON_GetArrayItem(json, i); name = cJSON_GetObjectItem(student, "name"); age = cJSON_GetObjectItem(student, "age"); grades = cJSON_GetObjectItem(student, "grades"); math = cJSON_GetObjectItem(grades, "math"); physics = cJSON_GetObjectItem(grades, "physics"); history = cJSON_GetObjectItem(grades, "history"); // Add strAdd(&stud_80_str, name->valuestring); strAdd(&stud_80_str, " "); strAdd(&stud_80_str, int2Str(&age->valueint)); strAdd(&stud_80_str, " "); strAdd(&stud_80_str, int2Str(&math->valueint)); strAdd(& stud_80_str, "-"); strAdd(&stud_80_str, int2Str(&physics->valueint)); strAdd(&stud_80_str, "-"); strAdd(&stud_80_str, int2Str(&history->valueint)); strAdd(&stud_80_str, "\n"); } // Get out data printf("Student with best average score:\n" "%s\n\n" "Students with 80+ math scores:\n" "%s", aver_best_str, stud_80_str); cJSON_Delete(json); free(aver_best_str); free(stud_80_str); return 0; }