开发者

JSONCPP Not reading files correctly

So I recently installed JSONCPP and for some reason it gives me errors when I try this code:

#include <json.h>
#include <iostream>
#include <fstream>

int main(){
    bool alive = true;
    while (alive){
    Json::Value root;   // will contains the root value after parsing.
    Json::Reader reader;
    std::string test = "testis.json";
    bool parsingSuccessful = reader.parse( test, root, false );
    if ( !parsingSuccessful )
    {
        // report to the user the failure and their locations in the document.
        std::cout  << reader.getFormatedErrorMessages()
               << "\n";
    }

    std::string encoding = root.get("encoding", "UTF-8" ).asString();
    std::cout << encoding << "\n";
    alive = false;


    }
    return 0;
}

And here is the file:

{
"encoding" : "lab"
}

It says that there is a syntax error on Line 1, Column 1, and that there must be a value, object or array开发者_开发问答. Anyone know how to fix this?

EDIT: Changed to current code, from pastebin


See the Json::Reader::parse documentation. For that overload, the string needs to be the actual document, not the filename.

You can use the istream overload with a ifstream instead.

std::ifstream test("testis.json", std::ifstream::binary);

EDIT: I got it work with:

#include "json/json.h"
#include <iostream>
#include <fstream>

int main(){
    bool alive = true;
    while (alive){
    Json::Value root;   // will contains the root value after parsing.
    Json::Reader reader;
    std::ifstream test("testis.json", std::ifstream::binary);
    bool parsingSuccessful = reader.parse( test, root, false );
    if ( !parsingSuccessful )
    {
        // report to the user the failure and their locations in the document.
        std::cout  << reader.getFormatedErrorMessages()
               << "\n";
    }

    std::string encoding = root.get("encoding", "UTF-8" ).asString();
    std::cout << encoding << "\n";
    alive = false;
    }
    return 0;
}


#include "json/json.h"
#include <iostream>
#include <fstream>

int main(){
    Json::Value root;   // will contain the root value after parsing.
    std::ifstream stream("testis.json", std::ifstream::binary);
    stream >> root;
    std::string encoding = root.get("encoding", "UTF-8" ).asString();
    std::cout << encoding << "\n";
    return 0;
}

Or more generally:

#include "json/json.h"
#include <iostream>
#include <fstream>

int main(){
    Json::Value root;   // will contain the root value after parsing.
    Json::CharReaderBuilder builder;
    std::ifstream test("testis.json", std::ifstream::binary);
    std::string errs;
    bool ok = Json::parseFromStream(builder, test, &root, &errs);
    if ( !ok )
    {
        // report to the user the failure and their locations in the document.
        std::cout  << errs << "\n";
    }

    std::string encoding = root.get("encoding", "UTF-8" ).asString();
    std::cout << encoding << "\n";
    return 0;
}

http://open-source-parsers.github.io/jsoncpp-docs/doxygen/namespace_json.html


json cannot contain newlines. Try this instead:

{"encoding": "lab"}

You may need to ensure the file is saved without a final newline.

EDIT: Maybe your parser tolerates newlines, but some don't. Something to try if the other answers don't work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜