-4

i am currently working in c++ for call application.i have one json response as string.i want to parse the json data. i have used boost packages for get json data. I want to take the users_list key from the json using boost.please reply the steps. thanks in advance.....

{"action":"refresh_dashboard","data":{"users_list":[{"user_id":"901e6076ff351cfc2195fb86f8438a26","extensions":["1002"],"name":"Karthik M"},{"user_id":"7d617ef5b2390d081d901b0d5cd108eb","extensions":["1015"],"name":"Synway User2"},{"user_id":"c8f667f7d663e81f6e7fa34b9296f067","extensions":["1012"],"name":"Rahib Video"},{"user_id":"cc3f94ecc14ee9c55670dcde9adc1887","extensions":["1006"],"name":"Rounak S Kiran"},{"user_id":"6c29ebdb34e1761fdf9423c573087979","extensions":["1003"],"name":"Amar Nath"},{"user_id":"8e15c2d95d4325cb07f0750846966be8","extensions":["1011"],"name":"TLS User"},{"user_id":"2fc4142bdacf83c1957bda0ad9d50e3d","extensions":["1014"],"name":"Synway User1"},{"user_id":"74d5b5a9aca1faa4c2f217ce87b621d8","extensions":["1008"],"name":"Robin Raju"},{"user_id":"a7ad7e73bf93ea83c8efdc1723cba198","extensions":["1007"],"name":"Arshad Arif"},{"user_id":"b55146df593ec8d09e5fe12a8a4c1108","extensions":["1001"],"name":"Rahib Rasheed"},{"user_id":"391391de005a8f5403c7b5591f462ea1","extensions":["1013"],"name":"Sangeeth J"},{"user_id":"3258f7ae4ae1db60435cbcf583f64a89","extensions":["1009"],"name":"Aby TL"},{"user_id":"90bc84e5e8a3427fe35e99bd4386de95","extensions":["1010"],"name":"Prince T"},{"user_id":"b501ef5b270a196afc0eed557ca74237","extensions":["1005"],"name":"Jineed AJ"},{"user_id":"1422af351e06adeab2de92f5a633a444","extensions":["1004"],"name":"Ashok PA"}],"busy_users":[],"reg_users":[{"user_id":"901e6076ff351cfc2195fb86f8438a26","status":"registered"},{"user_id":"6c29ebdb34e1761fdf9423c573087979","status":"registered"}],"contacts":[{"owner_id":"901e6076ff351cfc2195fb86f8438a26","status":"ready"},{"owner_id":"6c29ebdb34e1761fdf9423c573087979","status":"ready"}]}}

Prince
  • 277
  • 2
  • 16

2 Answers2

0

There are several C++ libraries out there which will help you parse json string. You could use jsoncpp. Then you can write something like:

Json::Value data;
Json::CharReaderBuilder reader;
std::string errs;
std::stringstream ss;
ss << inp_string;

if (Json::parseFromStream(reader, ss, &data, &errs))
{
    const Json::Value users_list = data["data"];
}

Use asString() method or something similar to extract fields from the users_list as a string.

skr
  • 914
  • 3
  • 18
  • 35
  • i have used jsoncpp . i have added packages from nuget.but after the build showing some build errors. That's the reason why i am using boost. – Prince Mar 16 '18 at 14:07
  • sir did you used boost . – Prince Mar 16 '18 at 14:09
  • I don't think `boost` was specifically mentioned while I was writing the answer. Sorry if I missed it. – skr Mar 16 '18 at 14:10
  • No, I haven't used `boost` for parsing `json`. – skr Mar 16 '18 at 14:10
  • i have tried this post.but in my case it is little difficult.https://stackoverflow.com/questions/31345401/parse-json-array-as-stdstring-with-boost-ptree – Prince Mar 16 '18 at 14:12
  • @skr_robo.please check this – Prince Mar 16 '18 at 14:12
  • Please provide more details about the "difficulty" you are facing. – skr Mar 16 '18 at 14:14
  • in my case, data":{"users_list":[{"user_id","sdsd" } ."userid" i want to take.if that userlist is not there,it will be easy to get that array – Prince Mar 16 '18 at 14:16
0
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using boost::property_tree::ptree;

int main() {
    std::string ss = "{ \"action\" : \"refresh_dashboard\", \"data\" : [{ \"user_id\" : \"901e6076ff351cfc2195fb86f8438a26\" }, { \"user_id\" : \"901e6076ff351cfc2195fb86f8438a26\" }, { \"data\" : \"901e6076ff351cfc2195fb86f8438a26\" }] }";

    ptree pt;
    std::istringstream is(ss);
    read_json(is, pt);

    std::cout << "id:     " << pt.get<std::string>("action") << "\n";

    for (auto& e : pt.get_child("data")) {
        std::cout << "stuff name: " << e.second.get<std::string>("user_id") << "\n";
    }
}
Prince
  • 277
  • 2
  • 16