微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – JSONCPP不正确读取文件

所以我最近安装了 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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐