gpt4 book ai didi

c++ - JSONCPP 没有正确读取文件

转载 作者:可可西里 更新时间:2023-11-01 15:03:54 31 4
gpt4 key购买 nike

所以我最近安装了 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 overloadifstream 代替。

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, 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;
}

关于c++ - JSONCPP 没有正确读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4273056/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com