加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – JSONCPP不正确读取文件

发布时间:2020-12-16 05:39:27 所属栏目:百科 来源:网络整理
导读:所以我最近安装了 JSONCPP,并且由于某些原因,当我尝试这个代码时,它给我错误: #include json.h#include iostream#include fstreamint main(){ bool alive = true; while (alive){ Json::Value root; // will contains the root value after parsing. Json::
所以我最近安装了 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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读