dropbox / json11

A tiny JSON library for C++11.
MIT License
2.55k stars 613 forks source link

How to parse array of lists ? #87

Closed Shravan40 closed 7 years ago

Shravan40 commented 7 years ago

I would like to parse this json object.

{
  "status": 1,
  "message": [
    {
      "name": "UTI Dual Advantage Fixed Term Fund Series I-IV (1099 Days) - Direct Plan - Dividend Option",
      "uploadDate": "2015-08-18",
      "value": 10.3842
    },
    {
      "name": "UTI Dual Advantage Fixed Term Fund Series I-IV (1099 Days) - Direct Plan - Dividend Option",
      "uploadDate": "2015-08-18",
      "value": 10.3842
    },
    {
      "name": "UTI Dual Advantage Fixed Term Fund Series I-IV (1099 Days) - Direct Plan - Dividend Option",
      "uploadDate": "2015-08-18",
      "value": 10.3842
    }]
}

Here is my try.

string input_error;
auto y = Json::parse(r1.text,input_error);
auto status = y["status"].int_value();
for(auto& i: y["message"].arry_items()) {
    // Now i don't know, what to do. 
}
Shravan40 commented 7 years ago

I have found a solution. But not sure, whether it the best way to do it.

        for(auto& k: y["message"].array_items()){
            auto z = Json::parse(k.dump(),input_error);
            auto fName = z["fund_name"].string_value();
            auto uDate = z["upload_date"].string_value();
            auto nav = z ["nav"].number_value();
        }
j4cbo commented 7 years ago

"Json::parse(k.dump(), ...)" is a no-op. Just use k["fund_name"].string_value() etc. On Mon, Dec 26, 2016 at 11:36 AM Shravan Kumar Gond < notifications@github.com> wrote:

I have found a solution. But not sure, whether it the best way to do it.

    for(auto& k: y["message"].array_items()){
        auto z = Json::parse(k.dump(),input_error);
        auto fName = z["fund_name"].string_value();
        auto uDate = z["upload_date"].string_value();
        auto nav = z ["nav"].number_value();
    }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dropbox/json11/issues/87#issuecomment-269225332, or mute the thread https://github.com/notifications/unsubscribe-auth/AAgyRlcMrc1pkdEEzxLpPjQ-ffI1Nak-ks5rL-0DgaJpZM4LVr14 .