<>Json

JSON: JavaScript Object Notation(JavaScript Object representation ), Is the syntax for storing and exchanging text information . It's similar XML, But more than
XML Smaller , Faster , Easier to analyze .

JSON Is a lightweight text data exchange format , Independent of language , It is self descriptive , Easier to understand .

JSON Text format is grammatically different from creation JavaScript The code of the object is the same , Because of this similarity , No parser required ,JavaScript The program can use the built-in eval()
function , use JSON Data to generate native JavaScript object .

<> rule of grammar

* Data in name / Value alignment
* Data is separated by commas
* Braces save objects
* Bracket save array
for example :
var sites = [ { "name":"foo" , "url":"www.foo.com" }, { "name":"google" , "url"
:"www.google.com" }, { "name":" micro-blog " , "url":"www.weibo.com" } ];
<>jsoncpp

c++ There are many ways to parse json data , include Jsoncpp and Boost Library property_tree.

Jsoncpp It's a cross platform open source library , It's used a lot .

download , compile , Please refer to relevant documents for installation .

<> read json file

* json The contents of the document are as follows : % cat test.json [{"name":" full name ", "age":27}]
* read json File source code : #include <fstream> #include <iostream> #include <json/json.h> #
include <cassert> #include <errno.h> #include <string.h> using namespace std;
int main(void) { ifstream ifs; ifs.open("test.json"); assert(ifs.is_open());
Json::Reader reader; Json::Value root; if (!reader.parse(ifs, root, false)) {
cout<< "reader parse error: " << strerror(errno) << endl; return -1; } string
name; int age; int size; size = root.size(); cout << "total " << size << "
elements" << endl; for (int i = 0; i < size; ++i) { name = root[i]["name"].
asString(); age = root[i]["age"].asInt(); cout << "name: " << name << ", age: "
<< age << endl; } return 0; }
* compile
g++ -std=c++11 -o json_test json_test.cpp -ljson % ./json_test total 1
elements name: full name , age: 27
* be careful , In this example json The file uses arrays , And only one element , So it's used in the program for Loop to read , You can add elements .
* If there is only one set of elements , You can directly use braces to form an object , Program can be read directly , as follows : string name = root["name"].asString(
); int age = root["age"].asInt(); cout << "name: " << name << ", age: " << age
<< endl;
* For the keyword plus object pattern json file , Its value can be read as follows : %cat test1.json { "beijing": { "gdp": 20,
"population": 200 }, "shanghai": { "gdp": 20, "population": 200 } } int bj_gdp =
root["beijing"]["gdp"].asInt(); int bj_population = root["beijing"][
"population"].asInt(); int sh_gdp = root["shanghai"]["gdp"].asInt(); int
sh_population= root["shanghai"]["population"].asInt();
<> write json file

* Source code static void write_json(string f) { Json::Value root; Json::FastWriter
writer; Json::Value person; person["age"] = 28; person["name"] = "sb"; root.
append(person); string json_file = writer.write(root); ofstream ofs; ofs.open(f)
; assert(ofs.is_open()); ofs << json_file; return; }
* The same compilation method , Got it json The document is as follows : % cat test_write.json [{"age":28,"name":"sb"}]

Technology