所以我最近安装了
JSONCPP,并且由于某些原因,当我尝试这个代码时,它给我错误:
#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; }
这里是文件:
{ "encoding" : "lab" }
它表示在第1行列1上存在语法错误,并且必须有一个值,对象或数组.任何人都知道如何解决这个问题?
编辑:从pastebin更改为当前代码
解决方法
请参阅
Json::Reader::parse
文档.对于该重载,字符串需要是实际文档,而不是文件名.
您可以使用istream overload与ifstream.
std::ifstream test("testis.json",std::ifstream::binary);
编辑:我得到它的工作:
#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,"UTF-8" ).asString(); std::cout << encoding << "\n"; alive = false; } return 0; }