C + + parsing json (JSONCPP)
JSON(JavaScript Object Notation)
is a lightweight data interchange format. It is based on a subset of JavaScript (standard ECMA-262 3rd edition-december 1999). JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (network transfer speed)
Here we mainly use jsoncpp
to parse the JSON file
Ready to work Download jsoncpp
We'll start by creating a new folder and jsonCpp
then cloning jsoncpp to our folder:
>mkdir jsoncpp && CD jsoncpp
>git Clone Https://github.com/open-source-parsers/jsoncpp.git
Now your folder will have a Jsoncpp folder, which is the source of Jsoncpp
Compiling jsoncpp
The face of this multiple files and folders, is not a bit confusing, but we use only two folders, a src/lib_json
folder, a include/json
folder
Then create a new directory and copy the two folders json
to the folder lib_json
Download we need to compile these files, and generate static link library, actually do not compile also line, just back to use a bit easier, we have to edit a Makefile
TARGET=libjson.libSRCS=json_writer.cpp json_reader.cpp json_value.cppOBJS=$(SRCS:.cpp=.o)INCS=-I json$(TARGET):$(OBJS) ar rv [email protected] $^%.o:%.cpp g++ -c $< $(INCS) -o [email protected]
I found an error after make.
The error shown is:
Error finding not to head file
Bo Master pondering, found still can not find the solution (to makefile not too familiar with the big God pointing it?) ), only change the source file, so will json_value.cpp
, json_reader.cpp
the json_write.cpp
three files inside the head file of the angle brackets changed to double quotation marks, and then make compiled successfully.
Reading JSON files
We got the Jsoncpp static link library libjson.lib, and then we started parsing the JSON file with Jsoncpp.
From the above compilation, the Jsoncpp contains roughly three classes:
- Json::value used to hold data that is read or to be written to a file
- Json::reader is used to read the data inside the JSON file and pass it into the Json::value object.
- Json::fastwriter is used to write Json::value objects to a JSON file.
Read a simple JSON file
Include <fstream>#include <cassert>#include "Json/json.h"#include <iostream>using namespace STD;intMain () {Ifstream inFile ("Test.json", ios::in);if(!infile.is_open ()) {Cerr<<"Open the file failed"<<endl;return-1; } Json::value Root; Json::reader Reader;if(!reader.parse (InFile, Root,false)) {Cerr<<"Parse file failed"<<endl;return-1; }cout<<"Name:"<<root["Name"].asstring () <<endl;cout<<"Age:"<<root["Age"].asint () <<endl;return-1;}
test.json
The data inside is:
{ "name":"qeesung", "age":21}
Let's compile: g++ main.cpp libjson.lib -o myJson
The result of the operation is:
To read a JSON file containing an array
#include <fstream>#include <cassert>#include "Json/json.h"#include <iostream>using namespace STD;intMain () {Ifstream inFile ("Test.json", ios::in);if(!infile.is_open ()) {Cerr<<"Open the file failed"<<endl;return-1; } Json::value Root; Json::reader Reader;if(!reader.parse (InFile, Root,false)) {Cerr<<"Parse file failed"<<endl;return-1; } for(inti =0; I<root.size (); ++i) {cout<<"Name:"<<root[i]["Name"].asstring () <<endl;cout<<"Age:"<<root[i]["Age"].asint () <<endl; }return-1;}
The JSON file is:
[{"name ": "qeesung1" , "age ": 21 }, {"name : "qeesung2" , " Age ": 22 }, {" name ": " Qeesung3 " , "age ": 23 }, {"name ": ," age ": }]
The results of the compilation run are:
Write data to son file
#include <fstream>#include <cassert>#include "Json/json.h"#include <iostream>using namespace STD;intMain () {Ofstream outFile ("Test.json", Ios::out | IOS::TRUNC);if(!outfile.is_open ()) {Cerr<<"Open the file failed"<<endl;return-1; } Json::value Root; Json::fastwriter writer; for(intK =0; K <3; ++k) {root[k]["Root"]="Qeesung"; root[k]["Age"]=k+Ten; }stringstr = writer.write (root); outfile<<str;return-1;}
We get the result:
[{"age":10,"root":"qeesung"},{"age":11,"root":"qeesung"},{"age":12,"root":"qeesung"}]
C + + parsing json (JSONCPP)