Search code examples
c++rapidjson

rapidjson : Extract a parameter from JsonObject using rapidjson


I have a jsonObject that looks like

{"Types":[{"Mtype":"text/plain","time":"Thus:24:32:02"},{"MtypeSec":"text/plain","time":"Thus:24:32:02"}]}

I wanted to know how I can extract Mtype and time ? is the Types a jsonArray ??


Solution

  • Looks like Types is an array, but arrays are a subclass of Object so that is why IsObject() returns true. You should call IsArray() on it to see if it is an array.

    The correct syntax would be document["Types"][0]["Mtype"].GetString(), or you can iterate over it with the following:

    for (SizeType i = 0; i < document["Types"].Size(); i++){
        std::string strval;
        if(document["Types"][i].hasMember("Mtype")){
            strval = document["Types"][i]["Mtype"].GetString();
        } else if(document["Types"][i].hasMember("mtypeSec")){
            strval = document["Types"][i]["mtypeSec"].GetString();
        } else if(document["Types"][i].hasMember("time")){
            strval = document["Types"][i]["time"].GetString();
        } else if(/*other member test*/){
            //do something with other member
        } else {
            //base case, member not in the list of useful members
        }
        //do something useful with strval
        ....
    }