VS Professional 2017 配置Jsoncpp

来源:互联网 发布:战地4淘宝怎么搜 编辑:程序博客网 时间:2024/06/16 03:21
  • Jsoncpp 下载链接
  • 将下图Jsoncpp目录中的.h文件 以及.cpp文件 移动到 项目 (需要使用jsoncpp的项目)的目录下

    • Jsoncpp - .h文件目录
    • Jsoncpp - .cpp文件目录
    • 需要使用jsoncpp的项目的目录

  • 在项目中将上述的.h文件添加到头文件下,.cpp文件添加到源文件下

  • 测试:

#include<iostream>#include"json.h"#include<fstream>using namespace std;int main() {    Json::Reader read;    Json::Value root;    std::ifstream is("G:/in.txt", ios::binary);    if (!is.is_open()) {        cout << "Error opening file\n";    }    else {        if (read.parse(is, root)) {            string str = root["abc"].asString();            cout << str << endl;        }    }    system("pause");}
  • in.txt

  • 编译运行后报错:

      1. 报错信息:
    • 解决方案: 将下图中画红线部分删除

      1. 报错信息:
    • 解决方案: 将下图#include < json/writer.h > 修改为 #include”writer.h” (原因: 路径变了, 其他类似的报错信息,同样处理)
  • 运行结果:

    注意: 原本json文件(in.txt) 只写了{ “abc”: “efg” } , 但是没任何输出(解析失败), 于是又加了一个 “age” : 0, 这才有结果, 可能是单个的情况下,不能称为Json吧。