Highly recommended a simple and easy to use C ++ JSON library

  Highly recommended a simple and easy to use C ++ JSON library CJsonObject, make use json as the use of C ++ native structure is so convenient, arbitrary. CJsonObject is a good C ++ JSON library, might be the most easy to use C ++ json library you've ever seen. CJsonObject open-source address is https://github.com/Bwar/CJsonObject and https://gitee.com/Bwar/CJsonObject . Released at the beginning of the open source blog article on CJsonObject use "lightweight easy to use C ++ JSON library CJsonObject" .

  CJsonObject revenue a year, there is no deliberate promotion, access to more than 130 star and 60 fork on GitHub, in fact had the CJsonObject open source and establish a relationship with cJSON the fork (in fact cJSON on github CJsonObject with nothing to do, CJsonObject use the cJSON is based) is to introduce some traffic to Bwar more than another effort to build an open source project on SourceForge modified versions based on older cJSON the Nebula . Unexpectedly, the effort to build and wrote several technical content of the blog post to promote the Nebula star regardless of the number or the number of fork CJsonObject less than (the number may be a relationship with the audience). Bwar always believed that the vast majority of scenarios using JSON, ease of use and efficiency of the development is the first one, instead of parsing performance. On ease of use, he said CJsonObject make JSON as C ++ native data structures are generally not too much, so do not avoid the suspicion again Huang Po praises highly recommended! CJsonObject good updates and maintenance, user response is very timely to mention the issue, open a year had increased the number of users but does not support the features they need.

  CJsonObject widely used in key open source projects Nebula Bwar in, regardless of their own or Bwar to outside developers, has to be maintained continuously updated. The way to play a little advertising, The Nebula is a powerful network IoC framework for message-driven applications in C ++ to quickly build high concurrency, distributed and resilient. Suitable for instant messaging, data acquisition, real-time calculation, push messaging, web services, and other background scenarios, Nebula has time production application case communications, data acquisition and real-time embedded point analysis. If you feel CJsonObject good, to Nebula also point a star, thank you.

  CJsonObject is based on newly developed cJSON a C ++ version of the JSON library, CJsonObject biggest advantage is lightweight, easy to use, high development efficiency. CJsonObject only four files, copy to your source code level integration can be, no need to compile into a library, and cross-platform and compiler. Unlike most json parsing library access nested json cumbersome different, CJsonObject nested json to read and generate very simple to use.

  For developers and CJsonObject issue blog project to raise this issue put together a FAQ as follows:

FAQ

  • 1. how to traverse the json key, and whichever value?
std::string strTraversingKey;
std::string strTraversingValue;
while(oJson.GetKey(strTraversingKey))
{
    if (oJson.Get(strTraversingKey, strTraversingValue))
    {
        std::cout << strTraversing << "  :  " << strTraversingValue << std::endl;
    }
}

  GetKey () does not apply to traverse the array, calling GetKey to json array () simply returns false. Call GetKey () function loops through to get the current level where json key, GetKey () returns false representation has been taken when the last key, the next traversal then call GetKey () will get the start again from the first key. Put another way, GetKey () traversal json key returns a value: true, true, true ... true, false; true, true, true ... true, false; true, true, true ... true, false; how much you want to traverse the wheel entirely up to the user.

  If you need a break through and start over, you can call ResetTraversing () function to reset the traverse.

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;
    }
}

// 因为上一个遍历中断时调用了ResetTraversing(),所以本次遍历又是从第一个key开始。如果上一个遍历中断时未调用ResetTraversing(),那这次遍历将是从上次终端的位置继续,这通常不是遍历的预期结果,因此,中断遍历时记得ResetTraversing()。
while(oJson.GetKey(strTraversingKey))
{
    if (oJson.Get(strTraversingKey, strTraversingValue))
    {
        std::cout << strTraversing << "  :  " << strTraversingValue << std::endl;
    }
}

  __ Note: __ to be Json current level of key Add () or Delete () operation, will lead to failure of the current iteration, the next call GetKey () will acquire key to start from scratch.

  • 2. Replace when a key, whether you need to replace the original value is consistent with the type of the value type?

  Replace () function key to be replaced, nothing to do with value types. To a value of the replacement value is int string, or Replace value or object array are possible. But as non-essential, we recommend the replacement value of the same type with the value of replacement type before.

  • 3. [] and () overloads What is the difference, why should these two overloaded operators?

  More () is overloaded Get () function series; [] or reload the operating JsonObject JsonArray for convenience to take down one level of nested json, does not apply to string, int basic types json convenient call, if quite sure key is there without going through get () return value to determine whether the acquisition is successful, call () encoding is faster than calling get (), is not applicable to JsonObject or JsonArray.

  [] And () return value is not the same, the two can not be mixed.

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

  CJsonObject of nested json operation is very flexible and convenient generation of nested json and reading There are many flexible usage.

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;

  Given here is only one writing, reference may be several other FAQ # 5.

  • 5. ask in an array using CJsonObject how to create the form?
{
    "employees": [
        { "firstName":"John" , "lastName":"Doe" },
        { "firstName":"Anna" , "lastName":"Smith" },
        { "firstName":"Peter" , "lastName":"Jones" }
    ]
}

  Here are three ways to generate the 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);

Guess you like

Origin www.cnblogs.com/bwar/p/11294452.html