json数组笔记

来源:互联网 发布:微信秒抢红包软件苹果 编辑:程序博客网 时间:2024/06/14 18:28

数组:

Json::Value root;Json::Value item;Json::Value arrayObj;root["Transcodec"] = "Transcodec";//root.append(item);for (int i = 0; i < 3; ++i){item["id"] = 1904967 + 1904967;if (0 == i){item["percent"] = 1904967;}else{item["percent"] = 1904967;}arrayObj.append(item);}root["array"] = arrayObj;std::string json = root.toStyledString();printf("%s\n", json.c_str());Json::Reader reader;Json::Value jValue;if (reader.parse((const char*)(json.c_str()), jValue)){int id, percent;Json::Value val_array = jValue["array"];printf("size = %d\n", val_array.size());for (int i = 0; i < val_array.size(); ++i){id = val_array[i]["id"].asInt();percent = val_array[i]["percent"].asInt();printf("id = %d, percent = %d\n", id, percent);}}


输出结果:

Json test begin...{   "Transcodec" : "Transcodec",   "array" : [      {         "id" : 3809934,         "percent" : 1904967      },      {         "id" : 3809934,         "percent" : 1904967      },      {         "id" : 3809934,         "percent" : 1904967      }   ]}size = 3id = 3809934, percent = 1904967id = 3809934, percent = 1904967id = 3809934, percent = 1904967


0 0