0

I tried but seems too many loops. Is there any optimal way?

Input JSON:

{"errors":[{"key":"XYZ","code":37373,"message":"Invalid XYZ Code"}]}

Hard code key in the solution, not an issue.

I have tried to print by below solution:

if(!aJsonDocument.Parse<0>(aResponseJson.c_str()).HasParseError())
{
    for(rapidjson::Value::ConstMemberIterator iter = aJsonDocument.MemberBegin(); iter != aJsonDocument.MemberEnd(); ++iter)
    {
        if(iter->name.IsString() && iter->value.IsArray())
        {
            std::string aKey = iter->name.GetString();
            const rapidjson::Value& aJsonData = aJsonDocument[aKey.c_str()];
            if(aJsonData.IsArray())
            {
                for (rapidjson::SizeType i = 0; i < aJsonData.Size(); i++)
                {
                    for(rapidjson::Value::ConstMemberIterator iter1 = aJsonData[i].MemberBegin(); iter1 != aJsonData[i].MemberEnd(); ++iter1)
                    {
                        std::string aKey = iter1->name.GetString();
                        boost::trim(aKey);
                        std::string aValue = iter1->value.GetString();
                        boost::trim(aValue);
                        std::cout<< "Key: " << aKey << ", Value:" << aValue << endl;;

                    }
                }
            }
        }
    }
}
gotnull
  • 26,454
  • 22
  • 137
  • 203
user2235747
  • 345
  • 6
  • 14

2 Answers2

1

You may firstly use JSON schema in RapidJSON to validate the JSON, so you don't need to hand-code all checkings such as IsObject(), IsArray(), etc.

Then, to iterate the array and objects in C++11, you can use range-based for:

for (auto& error : d["errors"].GetArray())
     for (auto& m : error.GetObject())
         std::cout << m.name.GetString() << ", " << m.value.GetString() << std::endl;
Milo Yip
  • 4,902
  • 2
  • 25
  • 27
0

Disclaimer I like ThorsSerializer; but I did write it so a bit biased. If it works for your needs, feel free to use it.

Parsing the code is as simple as declaring the objects you want to build.

Code

#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include <sstream>
#include <iostream>
#include <string>
#include <vector>

struct Error
{
    std::string             key;
    int                     code;
    std::string             message;
};
ThorsAnvil_MakeTrait(Error, key, code, message);

struct MyData
{
    std::vector<Error>      errors;
};
ThorsAnvil_MakeTrait(MyData, errors);

Example Usage:

int main()
{
    using ThorsAnvil::Serialize::jsonImport;
    using ThorsAnvil::Serialize::jsonExport;

    std::stringstream   inputData(R"({"errors":[{"key":"XYZ","code":37373,"message":"Invalid XYZ Code"}]})");

    MyData    object;
    inputData >> jsonImport(object);

    std::cout << object.errors[0].key       << "\n";
    std::cout << object.errors[0].code      << "\n";
    std::cout << object.errors[0].message   << "\n";

    std::cout << "-----\n";
    std::cout << jsonExport(object) << "\n";
}

Output:

XYZ
37373
Invalid XYZ Code
-----

    {
        "errors": [
            {
                "key": "XYZ",
                "code": 37373,
                "message": "Invalid XYZ Code"
            }]
    }
Martin York
  • 257,169
  • 86
  • 333
  • 562