Getting n objects and their field from JSon, then store them as class object

老子叫甜甜 提交于 2020-01-06 07:12:37

问题


Im trying to get information about my objects from JSon file. It contains n objects (2 for example) 4 fields each. I parse .json by rapidjson and my IDE is Qt Creator.

I already tried using Pointers desribed at http://rapidjson.org/md_doc_pointer.html#JsonPointer and Query Objects from their basic tutorial, but somehow I can't get it working.

that's how example .json file would look.

{
"opiekun1" : {
    "imie": "Maksym",
    "nazwisko": "Zawrotny",
    "email": "maksym@wp.pl",
    "haslo": "herbatka"},
"opiekun2" : {
    "imie": "Filip",
    "nazwisko": "Szatkowski",
    "email": "filip@wp.pl",
    "haslo": "kawusia"}
}

I get DOM Document by:

FILE* fp = fopen(json_filename.c_str(), "rb");
char readBuffer[65536];    
FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Document d;
d.ParseStream(is);    

I tried Pointer() like that:

Value* value = Pointer("/opiekun1/imie").Get(parsedJSon);

but I got:

invalid conversion from 'const rapidjson::GenericValue<rapidjson::UTF8<> >*' to 'rapidjson::Value* {aka rapidjson::GenericValue<rapidjson::UTF8<> >*}'

Mine another try was to iterate through objects in Document:

for (auto& object : parsedJSon.GetObject())
{
    CUzytkownik* user;
    user = new CUzytkownik;
    int counter = 0;
    for (Value::ConstMemberIterator itr = object.MemberBegin();
         itr != object.MemberEnd(); itr++)
    {
        if (itr->name.GetString() == "imie")
            user->imie = itr->value.GetString();

    }
}

But it says:

const struct rapidjson::GenericMember<rapidjson::UTF8<>, rapidjson::MemoryPoolAllocator<> >' has no member named 'MemberEnd'

I think that I misunderstand something about handling objects in .json files. Could anyone explain it to me and provide some example code? I would like my output to look something like that:

CUzytkownik* opiekun1 = new CUzytkownik;
opiekun1->name = "Maksym";
opiekun1->nazwisko = "Zawrotny";
opiekun1->email = "maksym@wp.pl";
opiekun1->haslo = "herbatka";

If anyone has experience with rapidjson and would like to help I will be most grateful. Any alternative examples like array handling or somethng like that ale most welcome too.

Thank you in advance!

来源:https://stackoverflow.com/questions/54256758/getting-n-objects-and-their-field-from-json-then-store-them-as-class-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!