JsonCpp is a lightweight data-interchange format. It can represent
numbers, strings, ordered sequences of values, and collections of
name/value pairs.
Step 1: Install using apt-get
You can easily install JsonCpp library on Ubuntu or another flavor of Debian Linux simply by running the following command:
sudo apt-get install libjsoncpp-dev
It will install the compatible JsonCpp library on your system.
Step 2: Example program and compiler flag
To compile a C++ program that uses JsonCpp library use the compiler flag:
-ljsoncpp
Create a Json file called profile.json with the following content:
{
"firstname":"Amritpal",
"lastname": "Singh",
"ss": 12345678910
}
Within the same directory create a userdata.cpp file with the following source code:
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>
using namespace std;
int main() {
ifstream ifs("profile.json");
Json::Reader reader;
Json::Value obj;
reader.parse(ifs, obj); // Reader can also read strings
cout << "Last name: " << obj["lastname"].asString() << endl;
cout << "First name: " << obj["firstname"].asString() << endl;
return 1;
}
Compile and Run the sample code:
g++ -o userdata userdata.cpp -ljsoncpp./userdata
The JsonCpp library has been successfully configured if you can execute the profile program.
Comments
Post a Comment