Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从C+;中的文件解析json时出错+;使用jsonCpp库_C++_Json_Jsoncpp - Fatal编程技术网

C++ 从C+;中的文件解析json时出错+;使用jsonCpp库

C++ 从C+;中的文件解析json时出错+;使用jsonCpp库,c++,json,jsoncpp,C++,Json,Jsoncpp,我有一个JSON文件之类的东西 [{ "movie_id": 1, "rating": "9.3", "votes": "1,318,626", "description": "Two imprisoned men bond over a number ....", "title": "The Shawshank Redemption", "poster": "", "release_date": "14 October 1994", "metascore": "

我有一个JSON文件之类的东西

[{ 
 "movie_id": 1, 
 "rating": "9.3", 
 "votes": "1,318,626", 
 "description": "Two imprisoned men bond over a number ....", 
 "title": "The Shawshank Redemption", 
 "poster": "", 
 "release_date": "14 October 1994", 
 "metascore": "80", 
 "director": "Frank Darabont", 
 "storyline": "Andy Dufresne is a young and successful ...",
 "stars": [ "Tim Robbins", "Morgan Freeman", "Bob Gunton" ], 
 "year": "1994", 
 "genre": [ "Crime", "Drama" ], 
 "gallery": [ "unknown1394846836._CB379391227_.png", ], 
 "running_time": "142min"
 },
 {...},
 {...},...]
我想使用jsonCpp库解析上面文件
input.json
中的数据,下面是我的代码

#include <bits/stdc++.h>
#include "json/json.h"
using namespace std;
int main(){
   Json::Value root;
   Json::Reader reader;

   ifstream file("input.json");
   file >> root;

   string title = root[0]["title"].asString();
   cout<<title;
   return 0;
}
这里可能有什么问题,如何解决


提前感谢

终于解决了!!我写我的问题的答案是为了帮助其他人 我没有正确地链接库,所以它给了我错误。你可以用C++的方式链接任何外部库。

  • 我的当前目录中有两个文件
    json/json.h
    jsoncpp.cpp
  • 创建一个新文件
    main.cpp
    ,并写下使用此库的代码
  • 不要编辑
    jsoncpp.cpp
  • 包括
    json.h
    文件和
    jsoncpp.cpp
    ,如下面我更新的代码所示
  • 然后运行
    g++main.cpp

    #include <bits/stdc++.h>
    #include "json/json.h"
    #include "jsoncpp.cpp"
    using namespace std;
    int main(){
        Json::Value root;
        Json::Reader reader;
    
        ifstream file("input.json");
        file >> root;
    
        string title = root[0]["title"].asString();
        cout<<title<<"\n";
        return 0;
    }
    

  • 终于解决了!!我写我的问题的答案是为了帮助其他人 我没有正确地链接库,所以它给了我错误。你可以用C++的方式链接任何外部库。

  • 我的当前目录中有两个文件
    json/json.h
    jsoncpp.cpp
  • 创建一个新文件
    main.cpp
    ,并写下使用此库的代码
  • 不要编辑
    jsoncpp.cpp
  • 包括
    json.h
    文件和
    jsoncpp.cpp
    ,如下面我更新的代码所示
  • 然后运行
    g++main.cpp

    #include <bits/stdc++.h>
    #include "json/json.h"
    #include "jsoncpp.cpp"
    using namespace std;
    int main(){
        Json::Value root;
        Json::Reader reader;
    
        ifstream file("input.json");
        file >> root;
    
        string title = root[0]["title"].asString();
        cout<<title<<"\n";
        return 0;
    }
    

  • 听起来你没有链接库?我想我已经链接了,但是为了确保你能简要告诉我链接库的过程吗?听起来你没有链接库?我想我已经链接了,但是为了确保你能简要告诉我链接库的过程吗?
    The Shawshank Redemption