如何从json文件中填充对象数组

时间:2017-03-12 20:13:09

标签: c json

我不知道如何从json文件中填充数组元素:

[
    {
        "name": "Taras",
        "surname": "Shevchenko",
        "age": 14,
        "experience": 4.6,
        "budget": 100,
        "startup": [
            {
                "name": "someField"
            },
            {
                "name": "hummm"
            }
        ]
    },
    {
        "name": "Yuriy",
        "surname": "Brusentsov",
        "age": 17,
        "experience": 4.3,
        "budget": 100500,
        "startup": [
            {
                "name": "KP-61"
            },
            {
                "name": "KP-62"
            }
        ]
    }
]  

我想填充一系列结构" startup",但我不知道如何使用json文件和C正确。

这是我的计划:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <jansson.h>

#define NUMBER_OF_DIRECTORS 10
#define NUMBER_OF_STARTUPS 4

typedef struct Startup {
    char * name;
} Startup;

typedef struct Director {
    char *  name;
    char *  surname;
    int     age;
    float   experience; 
    int     budget;
    Startup startup[NUMBER_OF_STARTUPS];
} Director;


void readJson() {
        char jsonStr[1000] = "";
        readAllText("directors.json", jsonStr);

        Director dir[10];
        json_error_t err;
        json_t * jsonArr = json_loads(jsonStr, 0, &err);
        int index = 0;
        json_t * value = NULL;
        //json_t* root = json_loadf(fJSON, 0, &error);
        json_array_foreach(jsonArr, index, value) {
            printf("\n\nStudent %i:\n", index);
            dir[index].name = (char *)json_string_value(json_object_get(value, "name"));
            printf("\t%10s: %s\n", "name", dir[index].name);
            dir[index].surname = (char *)json_string_value(json_object_get(value, "surname"));
            printf("\t%10s: %s\n", "surname", dir[index].surname);
            dir[index].age = json_integer_value(json_object_get(value, "age"));
            printf("\t%10s: %i\n", "age", dir[index].age);
            dir[index].experience = json_real_value(json_object_get(value, "experience"));
            printf("\t%10s: %.2f\n", "experience", dir[index].experience);
            dir[index].budget = json_integer_value(json_object_get(value, "budget"));
            printf("\t%10s: %i\n", "budget",dir[index].budget);
            //printf("WORKS!");

            for (int i = 0; i < NUMBER_OF_STARTUPS; i++)
            {
                if (dir[index].startup[i].name != NULL)
                {
                    json_t * startupObj = json_object();
                    json_t * startup = json_object_get(startupObj, "startup");
                    json_t * startupArr = json_array_get(startup, i);
                    dir[index].startup[i].name = (char *) json_string_value(json_object_get(startupArr, "name"));
                    printf("\t%10s: %s\n", "startup", dir[index].startup[i].name);
                    //json_array_append_new(startupArr, startupObj);
                }
            }
        }
        index++;

        json_decref(jsonArr);
    }

int main(void) {
    readJson();
    printf("\n\nthis is json file:\n");
    writeJson();


    return 0;
}


void readAllText(const char * filename, char * text) {
    char line[100];

    FILE * fr = fopen(filename, "r");
    while(fgets(line, 100, fr)) {
        strcat(text, line);
    }
    fclose(fr);
}

有一个主要时刻:

if (dir[index].startup[i].name != NULL)
{
    json_t * startupObj = json_object();
    json_t * startup = json_object_get(startupObj, "startup");
    json_t * startupArr = json_array_get(startup, i);
    dir[index].startup[i].name = (char *)        json_string_value(json_object_get(startupArr, "name"));
    printf("\t%10s: %s\n", "startup", dir[index].startup[i].name);
    //json_array_append_new(startupArr, startupObj);
}

在输出中我有一个&#34; null&#34; on&#34;初创公司&#34;。

感谢您的关注,我希望有人会帮助我。

0 个答案:

没有答案