Savita
Savita

Reputation: 75

How to get JSON objects value using casablanca in C++

I am new to Json. And i m using codeplex (casablanca) for reading the json values.

Below is the sample json string:

[{ "rollno": 2000,
"name": "suhani","marks":{"grade":"C"}  },  {"rollno": 3000,"name": "ishu", "marks":{ "grade":"A"}  }]

The code to access name & rollno, i am writing below code:

json::value jobj = json::value::parse(utility::conversions::to_string_t(resultbody));

for (unsigned int i = 0; i < jobj.size(); i++) {
    auto getval = jobj[i];

    if (getval.at(U("name")).is_string()) {
    auto xstr = getval.at(U("name")).as_string();
    std::string wide = utility::conversions::to_utf8string(xstr);
    std::string str(wide.begin(), wide.end());
    string name = str;
}

if (getval.at(U("rollno")).is_integer()) {
    auto xstr = getval.at(U("rollno")).as_integer();
    int rollno = xstr;
} }

HOW TO GET VALUE AT GRADE ?? When i access marks it is of type object, i am not getting how to access grade from there. Please respond.

Upvotes: 2

Views: 1431

Answers (1)

David Rinck
David Rinck

Reputation: 6996

Marks is still a json object. You'll need to access the grade property. From your code snippet add the following:

    for (unsigned int i = 0; i < jobj.size(); i++) {
        auto getval = jobj[i];
        auto marks_object = getval.at(U("marks"));
        auto grade_value = marks_object.at(U("grade")).as_string();

Upvotes: 3

Related Questions