C++ USES the JsonCpp library to manipulate json formatted data samples

  • 2020-05-24 05:57:27
  • OfStack

This article illustrates an example of how C++ USES the JsonCpp library to manipulate data in json format. I will share it with you for your reference as follows:

preface

JSON is a lightweight data definition format that is much easier to learn than XML and no less extensible than XML, making it a good choice for data exchange

JSON all referred to as: JavaScript Object Notation, just as its name implies, JSON is used for marking javascript object, details refer to http: / / www json. org /.

In this paper, the third library JsonCpp is selected to parse json. JsonCpp is a well-known c++ parsing library, which is also first promoted on the official website of json.

JsonCpp profile

JsonCpp mainly includes three types of class:Value Reader Writer.

All object and class names in jsoncpp are in namespace json, including json.h.

Note: Json::Value can only handle strings of type ANSI. If C++ program encodes with Unicode, it is better to add 1 Adapt class to fit it.

Download and compile

The operating environment of this paper is: Redhat 5.5 + g++version 4.6.1 + GNU Make 3.81 + jsoncpp 0.5.0

Download the address is: http: / / sourceforge net/projects/jsoncpp /

After unzip, we get the jsoncpp-src-0.5.0 folder. We only need the jsoncpp header file and cpp file, jsonscpp header file is located at jsoncpp-src-0.5.0 \include\json,jsoncpp cpp file is located at jsoncpp-src-0.5.0 \src\lib_json.

Here I list our working directory:

jsoncpp/ // working directory
|-- include // header root
| |-- json //json header file, corresponding to jsoncpp-src-0.5.0 \include\json
|-- src //cpp source file root directory
|-- json //jsoncpp source file, corresponding to jsoncpp-src-0.5.0 \src\lib_json
|-- main.cpp // our main function calls the sample code for jsoncpp
|-- makefile //makefile, we do not need to say more, please see the site related makefile usage practice

Deserialize the Json object

Suppose there is an json object as follows:


{
  "name": "json " ,
  "array": [
    {
      "cpp": "jsoncpp"
    },
    {
      "java": "jsoninjava"
    },
    {
      "php": "support"
    }
  ]
}

We want to implement the anti-serial number code of json as follows:


void readJson() {
  using namespace std;
  std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
  Json::Reader reader;
  Json::Value value;
  if (reader.parse(strValue, value))
  {
    std::string out = value["name"].asString();
    std::cout << out << std::endl;
    const Json::Value arrayObj = value["array"];
    for (unsigned int i = 0; i < arrayObj.size(); i++)
    {
      if (!arrayObj[i].isMember("cpp"))
        continue;
      out = arrayObj[i]["cpp"].asString();
      std::cout << out;
      if (i != (arrayObj.size() - 1))
        std::cout << std::endl;
    }
  }
}

Serialize Json objects


void writeJson() {
  using namespace std;
  Json::Value root;
  Json::Value arrayObj;
  Json::Value item;
  item["cpp"] = "jsoncpp";
  item["java"] = "jsoninjava";
  item["php"] = "support";
  arrayObj.append(item);
  root["name"] = "json";
  root["array"] = arrayObj;
  root.toStyledString();
  std::string out = root.toStyledString();
  std::cout << out << std::endl;
}

Click here to download the complete example code.

After downloading, execute the following command


unzip jsoncpp.zip
cd jsoncpp
make
./main

PS: for the operation of json, here are some useful json online tools for your reference:

Online JSON code inspection, inspection, beautification and formatting tools:
http://tools.ofstack.com/code/json

JSON online formatting tool:
http://tools.ofstack.com/code/jsonformat

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

json code online formatting/beautification/compression/editing/conversion tools:
http://tools.ofstack.com/code/jsoncodeformat

Online json compression/escape tool:
http://tools.ofstack.com/code/json_yasuo_trans

I hope this article is helpful to you C++ programming.


Related articles: