Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. load a valid json file in app use rapidjson show error : 'Error(offset 0): The document is empty.'
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 709 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Princein
    wrote on last edited by
    #1

    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!

    VRoninV 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • P Princein

        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!

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        @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!
        }
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved