Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

load a valid json file in app use rapidjson show error : 'Error(offset 0): The document is empty.'



  • I developed a project in Mac Qt which transplanted from windows qt, I want to load a JSON file in Desktop for test,in my project I use the 'loadJsonFile' method , when running the app, it shows me the error :
    Error(offset 0): The document is empty.
    but the fuconfig.json has valid content,

    here is the 'loadJsonFile' method

    void loadJsonFile(){
       string fuconfig_pathStr = "/Users/lee/Desktop/fuconfig.json";
        cout << "jsonfilePathStr_test--" << fuconfig_pathStr << endl;
       FILE* fp = fopen(fuconfig_pathStr.c_str(), "r"); // non-Windows use "r"
       char readBuffer[65536];
       FileReadStream is(fp, readBuffer, sizeof(readBuffer));
       Document d;
       d.ParseStream(is);
        if (d.HasParseError()) {
            fprintf(stderr, "\nError(offset %u): %s\n",
                (unsigned)d.GetErrorOffset(),
                GetParseError_En(d.GetParseError()));
        }
    }
    

    so I craete a new qt project to test the method, in the new qt project, I called the 'loadJsonFile' method,it worked fine, I don't know why,can you give me some tips, thanks a lot!



  • Maybe a silly question, but did you check with the debugger the file is opened? The shown snippet seems to assume the file is there and valid. Your path looks weird to me.



  • @Princein said in load a valid json file in app use rapidjson show error : 'Error(offset 0): The document is empty.':

    I developed a project in Mac Qt which transplanted from windows qt,

    The above is 0% Qt. The Qt version of the loadJsonFile() is:

    QFile jsonFile(QDir(QStandardPaths::standardLocations(QStandardPaths::DesktopLocation)).filePath("fuconfig.json"));
    if(jsonFile.open(QFile::ReadOnly)){
    QJsonParseError parseErr;
    QJsonDocument doc = QJsonDocument::fomJson(jsonFile.readAll(),&parseErr);
    if(parseErr.error == QJsonParseError::NoError){
    // All OK!
    }
    }
    

Log in to reply