利用boost 自带的json库,解析数组,json格式如下:
{ "roomId":"room_123", "pubUid":"uid1", "subList":[ "uid2", "uid3", "uid4" ] }
解析代码:
#include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { string json_info = json数据; boost::property_tree::ptree parse_root; boost::property_tree::ptree positions_array; std::stringstream strStream(json_info); boost::property_tree::read_json(strStream, parse_root); string room_id, pubUid; vector vec_subId; try { room_id = parse_root.get("roomId"); pubUid = parse_root.get("pubUid"); positions_array = parse_root.get_child("subList"); boost::property_tree::ptree::iterator pos = positions_array.begin(); for (; pos != positions_array.end(); ++pos) { boost::property_tree::ptree item = pos->second; string uid = item.get(""); vec_subId.push_back(uid); } } catch (boost::property_tree::ptree_error const &e) { return 0; } }