2

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.

David Rinck
  • 6,637
  • 4
  • 45
  • 60
Savita
  • 75
  • 4
  • [http_request::extract_json()](https://msdn.microsoft.com/de-de/library/jj969424.aspx) might also make things easier for you – x29a Nov 19 '15 at 12:24

1 Answers1

3

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();
David Rinck
  • 6,637
  • 4
  • 45
  • 60