Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONCPP Not reading files correctly

Tags:

c++

json

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

like image 910
Yelnats Avatar asked Nov 25 '10 01:11

Yelnats


2 Answers

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;
}
like image 178
Matthew Flaschen Avatar answered Oct 23 '22 05:10

Matthew Flaschen


#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

like image 4
cdunn2001 Avatar answered Oct 23 '22 04:10

cdunn2001