A simple and easy C++JSON library is highly recommended

_We strongly recommend a simple and easy-to-use C++JSON library, CJsonObject, to make using json as convenient as using C++ native structure. CJsonObject It's an excellent C++ JSON library, probably the simplest C++ JSON library you've ever seen.The open source address for CJsonObject is https://github.com/Bwar/CJsonObject and https://gitee.com/Bwar/CJsonObject.

_CJsonObject has been open source for one year, has not been promoted intentionally, has more than 130 star s and more than 60 forks on GitHub. In fact, CJsonObject was originally opened source and established a fork relationship with cJSON (in fact, CJsonObject has nothing to do with cJSON on github, CJsonObject uses cJSON based on an older version of cJ on SourceForge).SON based) is another open source project built to bring in more traffic to Bwar s Nebula .Unexpectedly, several technical blog posts were written to promote Nebra with fewer star ts or fork s than CJsonObjects (probably related to the number of audiences).Bwar always believes that ease of use and development efficiency are the first priority, not parsing performance, in most scenarios where JSON is used.In terms of ease of use, it is not too easy to say that CJsonObject makes JSON like C++ native data structure, so it is highly recommended to avoid the suspicion of selling cucumbers!CJsonObject is well updated and maintained, responds very promptly to issue s submitted by users, and adds many features that are not supported but needed by users during the year.

_CJsonObject is heavily used in Nebula, Bwar's key open source project, and needs to be continuously updated and maintained by both Bwar itself and external developers.By the way, for a small advertisement, Nebula Is a powerful IoC network framework for building concurrent, distributed, and resilient message-driven applications in C++.For instant messaging, data collection, real-time computing, message push, web background services and other scenarios, Nebula has production applications such as instant messaging, buried data collection and real-time analysis.If you think CJsonObject is good, give Nebula Or a star, thank you.

  CJsonObject Based on cJSON, a new JSON library is developed in C++. The biggest advantage of CJsonObject is lightweight, simple and easy to use, and highly efficient.CJsonObject has only four files and can be copied into its own code for source-level integration without compiling into libraries and across platforms and compilers.Unlike most JSON parsing libraries, which have a lot of trouble accessing multi-tier nested jsons, CJsonObject is very simple to read and generate multi-tier nested jsons.

_A FAQ has been compiled for the questions posed by developers on issue in blogs and CJsonObject projects as follows:

FAQ

  1. How do I iterate through json's key and take its value?
std::string strTraversingKey;
std::string strTraversingValue;
while(oJson.GetKey(strTraversingKey))
{
    if (oJson.Get(strTraversingKey, strTraversingValue))
    {
        std::cout << strTraversing << "  :  " << strTraversingValue << std::endl;
    }
}

_GetKey() traversal is not applicable to arrays, and calling GetKey() on a json array will return false directly.Call the GetKey() function to iterate through to get the json key at the current level. GetKey() returns false to indicate that the last key has been taken. Calling GetKey() again at the next iteration will start fetching from the first key.In other words, GetKey() traverses json key and returns true, true, true... true, false; true, true... true, false; true, true... true, false; how many rounds you want to traverse is entirely up to the user.

_If you need to interrupt a traversal and start over again, you can call the ResetTraversing() function to reset the traversal.

std::string strTraversingKey;
std::string strTraversingValue;
while(oJson.GetKey(strTraversingKey))
{
    if (strTraversingKey == "Auguest")
    {
        oJson.ResetTraversing();
        break;
    }
    if (oJson.Get(strTraversingKey, strTraversingValue))
    {
        std::cout << strTraversing << "  :  " << strTraversingValue << std::endl;
    }
}

// Since ResetTraversing() was called during the last traversal break, this traversal starts with the first key again.If ResetTraversing() was not called during the last traversal interrupt, the traversal will continue from the location of the last terminal, which is usually not the expected result of the traversal, so remember ResetTraversing() when interrupting traversal.
while(oJson.GetKey(strTraversingKey))
{
    if (oJson.Get(strTraversingKey, strTraversingValue))
    {
        std::cout << strTraversing << "  :  " << strTraversingValue << std::endl;
    }
}

Note: u Add() or Delete() operations on Json's current level key will invalidate the current traversal and the next call to GetKey() will get the key from scratch.

  1. When Replace a key, do you want the original value type to be the same as the replacement value type?

The_Replace() function replaces the key, regardless of the value type.It is possible to replace a value with an int with a string, or a value with an object or array.However, if it is not necessary, it is recommended that the value type after replacement be the same as the value type before replacement.

  1. What is the difference between the overload of [] and (), and why do you overload the two operators?

The overload of[] operates on JsonObject or JsonArray. To facilitate the removal of nested jsons layer by layer, it is not applicable to basic json types such as string and int; the overload of () is a more convenient call to Get() series functions, and if it is absolutely certain that key exists, it is not necessary to judge success by the return value of Get(), adjustEncoding with () is faster than calling Get (), and is not appropriate for manipulating JsonObject or JsonArray.

The return values of[] and () are different and cannot be mixed.

  1. How do I create a two-dimensional array similar to the following with CJsonObject?
{
    "test":[
        [{"test":1}],
        [{"test":2}]
    ]
}

_CJsonObject is very flexible and convenient for the operation of multi-layer nested jsons, and there are many flexible uses for the generation and reading of nested jsons.

neb::CJsonObject oTest;
oTest.AddEmptySubArray("test");
for (int i = 1; i < 3; ++i)
{
    neb::CJsonObject oDimension1;
    neb::CJsonObject oDimension2;
    oDimension2.Add("test", i);
    oDimension1.Add(oDimension2);
    oTest["test"].Add(oDimension1);
}
std::cout << oTest.ToString() << std::endl;

_Here is only one of the ways to write, the other can refer to FAQ#5.

  1. How do you create arrays in the following form using CJsonObject?
{
    "employees": [
        { "firstName":"John" , "lastName":"Doe" },
        { "firstName":"Anna" , "lastName":"Smith" },
        { "firstName":"Peter" , "lastName":"Jones" }
    ]
}

_Here are three ways to generate the above json array:

neb::CJsonObject oJson;
oJson.AddEmptySubArray("employees");
oJson["employees"].Add(neb::CJsonObject("{\"firstName\":\"John\" , \"lastName\":\"Doe\"}"));
oJson["employees"].Add(neb::CJsonObject("{\"firstName\":\"Anna\" , \"lastName\":\"Smith\"}"));
oJson["employees"].Add(neb::CJsonObject("{\"firstName\":\"Peter\" , \"lastName\":\"Jones\"}"));
neb::CJsonObject oJson;
oJson.AddEmptySubArray("employees");
oJson["employees"].Add(neb::CJsonObject("{}"));
oJson["employees"][0].Add("firstName", "John");
oJson["employees"][0].Add("lastName", "Doe");
oJson["employees"].Add(neb::CJsonObject("{}"));
oJson["employees"][1].Add("firstName", "Anna");
oJson["employees"][1].Add("lastName", "Smith");
oJson["employees"].Add(neb::CJsonObject("{}"));
oJson["employees"][2].Add("firstName", "Peter");
oJson["employees"][2].Add("lastName", "Jones");
neb::CJsonObject oJson;
neb::CJsonObject oJohn;
neb::CJsonObject oAnna;
neb::CJsonObject oPeter;
oJohn.Add("firstName", "John");
oJohn.Add("lastName", "Doe");
oAnna.Add("firstName", "Anna");
oAnna.Add("lastName", "Smith");
oPeter.Add("firstName", "Peter");
oPeter.Add("lastName", "Jones");
oJson.AddEmptySubArray("employees");
oJson["employees"].Add(oJohn);
oJson["employees"].Add(oAnna);
oJson["employees"].Add(oPeter);

Keywords: PHP JSON github network

Added by ayok on Sat, 03 Aug 2019 07:45:46 +0300