dropbox / json11

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

How to parse array of type string send as json object ? #84

Closed Shravan40 closed 7 years ago

Shravan40 commented 7 years ago

Json data send to post method given below is

{
"user_id":1,
"name": ["Alice","Bob","Peter"]
}
    CROW_ROUTE(app,"/saveName")
               .methods("POST"_method)
    ([](const crow::request& req){
        string input_error;
        auto x= Json::parse(req.body, input_error);
        if(x == nullptr)
            crow::response(400);
        auto userId = x["user_id"].int_value();
        std::vector<string> inp;
        for(auto& j: x["name"].array_items()) {
            inp.push_back(j.dump());
        }
    });

And it's parsing name in vector inp as "Alice","Bob" and "Peter" but i want to parse it as Alice,Bob and Peter.

j4cbo commented 7 years ago

dump() on a Json string will escape it and add quotes. I suspect you want inp.push_back(j); in the for loop?

Shravan40 commented 7 years ago
      for(auto& j: x["name"].array_items()) {
           auto val = j.string_value(); // It can be int_value(), number_value() too.
            inp.push_back(j.dump());
       }