Search code examples
c++jsonjsoncpp

Error While Parsing json from a file in C++ using jsonCpp Library


I have a JSON file somthing like that

[{ 
 "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"
 },
 {...},
 {...},...]

I want to parse data from above file input.jsonusing jsonCpp library and here is the my Code

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

and I used amalgamate.py to genrate jsoncpp.cpp and json.h files when I am running this code(jsoncpp.cpp) using command g++ jsoncpp.cpp I am getting the following error

/tmp/ccx18K5p.o: In function `main':
jsoncpp.cpp:(.text+0x19): undefined referen to Json::Value::Value(Json::ValueType)'
jsoncpp.cpp:(.text+0x28): undefined reference to `Json::Reader::Reader()'
jsoncpp.cpp:(.text+0x57): undefined reference to `Json::operator>>   (std::istream&, Json::Value&)'
jsoncpp.cpp:(.text+0x68): undefined reference to `Json::Value::operator[] (int)'
jsoncpp.cpp:(.text+0x75): undefined reference to  `Json::Value::operator[](char const*)'
jsoncpp.cpp:(.text+0x8a): undefined reference to `Json::Value::asString[abi:cxx11]() const'
jsoncpp.cpp:(.text+0xdc): undefined reference to `Json::Value::~Value()'
jsoncpp.cpp:(.text+0x12b): undefined reference to `Json::Value::~Value()'
collect2: error: ld returned 1 exit status

What could be the problem here and how can be resovled that ?

Thanks in advance


Solution

  • Finally Resolved !! I am writing answer to my question to help other I did not linked library properly so it gives me that error.You can link any external library in this way in c++.

    1. I have two file json/json.h and jsoncpp.cpp in my current directory
    2. Create a new file main.cpp and write down your code which used this library
    3. Do not edit jsoncpp.cpp
    4. include both json.h file and jsoncpp.cpp as shown in my updated code below.
    5. then Just run 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;
      }
      

      Output after run g++ main.cpp :

      The Shawshank Redemption