问题
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