Example of C + + parsing JSON using JSON CPP



For some time, XML became the data transmission format standard in the Internet industry, but some people questioned XML and thought that XML data format was complex and lengthy, so a new presentation format JSON was proposed.

The JSON format will not be described in detail here. The following mainly discusses the use of jsoncpp, a tool for C + + parsing JSON files.

JSON (JavaScript object notation) is a lightweight data exchange format, similar to XML. This paper mainly records the method of parsing JSON using jsoncpp in VS2008.
Jsoncpp is a cross platform open source library. Download address: http://sourceforge.net/projects/jsoncpp/ 。

Decompression:tar -zvxf jsoncpp-src-0.5.0.tar.gz

Enter the jsoncpp decompression directory and execute the command:

sudo scons platform=linux-gcc

Copy the JSON folder in the / JSON cpp-src-0.5.0/include/ directory to / usr / local / include/

Set libjson in the jsoncpp-src-0.5.0/libs/linux-gcc-4.9.1/ directory_ linux-gcc-4.9.1_ Copy libmt. A to / usr / local / lib / and rename it libjson. A for ease of use

Jsoncpp instructions:

The following describes the main interface functions in jsoncpp through a simple case. Through these basic functions, the parsing and processing of JSON files can be completed.

Main classes in jsoncpp:

  • JSON:: Value: can represent all supported types, such as int, double, string, object, array, etc. It includes functions such as node type judgment (isnull, isbool, isint, isarray, ismember, isvalidindex, etc.), type acquisition (type), type conversion (asint, asstring, etc.), node acquisition (get, []), node comparison (overload <, < =, >, > =, = =,! =), node operation (compare, swap, removemember, removeindex, append, etc.).
  • JSON:: Reader: parses a file stream or string into JSON:: value, mainly using the parse function. The constructor of JSON:: reader also allows the user to customize the severity level of JSON using the feature features.
  • JSON:: Writer: Contrary to jsonreader, it converts JSON:: value into string stream, etc. the writer class is a pure virtual class and cannot be used directly. Here, we use the subclasses of JSON:: Writer: JSON:: fastwriter (write data to a row without format), and JSON:: styledwriter (format output by JSON, easy to read).
  • The JSON:: reader can obtain a parsed JSON:: value by parsing the JSON source target. Usually, a string or file input stream can be used as the source target.

JSON example:

 { "name": "json",
    { "line": "1" },
    { "line": "2" },
    { "cpp": "jsoncpp" },
    { "java": "jsoninjava" },
    { "php": "support" }
 { "name": "c++",
    { "line": "3" },
    { "line": "4" },
    { "cpp": "jsoncpp" },
    { "java": "jsoninjava" },
    { "php": "nosupport" }

C + + Code:

#include <iostream>
#include <fstream>
#include <string>
#include "../src/json/json.h"
using namespace std;
using namespace Json;
void main()
  fstream ofile("json.json");
  string strjson;
  if (!ofile.is_open()) {
  string strline;
  while (getline(ofile, strline)) {
   strjson += strline;
  Json::Reader reader;  //  Reader 
  Json::Value root;    //  The value of value can be any object
  if (reader.parse(strjson, root)) {
    int size = root.size();   //  Number of root nodes
    for (int j = 0; j < size; j++) {
      cout << root[j]["name"].asString() << endl;
      const Json::Value arrayObj = root[j]["lines"];
      for (int i = 0; i < arrayObj.size(); i++) {
        if (arrayObj[i].isMember("line")) {
          cout << arrayObj[i]["line"].asString() << endl;
        if (arrayObj[i].isMember("cpp")) {
          cout << arrayObj[i]["cpp"].asString() << endl;
        if (arrayObj[i].isMember("java")) {
          cout << arrayObj[i]["java"].asString() << endl;
        int m = 0;

This is the end of this article about the method example of C + + using jsoncpp to parse JSON. For more information about C + + jsoncpp parsing JSON, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!