Storing JSON object in C ++ - map

I need help from you guys.

I am currently using cJSON to parse Json formatted data.

Below are the data below.

{
    "time" : {              
        "timezone" : string,
        "ntpservers" : array<string>
    },
    "datetime" : {
        "value" : "2013-10-23 03:35:45.182042",
        "space-custom-class" : "datetime.datetime"
    }
}

The problem I am facing is that I don’t know how to split the data into pairs of key values ​​and save them on the map. I see that "time" is the key, but the value is a dictionary, so how to store it as the value "time", and this applies to the key "datetime". I know there is some kind of iteration, but I'm stuck. Since a value can be a primitive type, an array, or a dictionary, should it be a general object, such as a template? Some example will be very helpful.

Thanks in advance for your help.

+4
4

JSON.org - ++ JSON, .

+4

, , .

Qt5 , JSON , . :

#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>

int main(int argc, char *argv[])
{
    QJsonObject topQuery;
    topQuery["objectType"] = QString("objects.Word");

    QJsonObject parameters;
    parameters["language"] = QString("en");
    parameters["to"] = QString("ru");
    parameters["rate"] = 0;
    parameters["isInput"] = true;

    topQuery["query"] = parameters;

    QJsonObject sortParameter;
    sortParameter["sortBy"] = QString("rate");
    sortParameter["direction"] = QString("desc");

    QJsonArray sortArray;
    sortArray.append(sortParameter);

    topQuery["sort"] = sortArray;
    topQuery["limit"] = 10;

    QJsonDocument document(topQuery);
    qDebug() << document.toJson();
}

:

{
    "limit": 10,
    "objectType": "objects.Word",
    "query": {
        "isInput": true,
        "language": "en",
        "rate": 0,
        "to": "ru"
    },
    "sort": [
        {
            "direction": "desc",
            "sortBy": "rate"
        }
    ]
}

Qt4 simular api: http://qjson.sourceforge.net/.

, ++ lib json, stl:: map, Qt.

0

, . cJson:

cJSON *current_element = NULL;
char *current_key = NULL;

cJSON_ArrayForEach(current_element, object)
{
    current_key = current_element->string;
    if (current_key != NULL)
    {
        /* do something with the key */
    }
}

: https://github.com/DaveGamble/cJSON/issues/167

0
source

All Articles