json数组笔记

    xiaoxiao2021-03-25  8

    数组:

    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 = 3 id = 3809934, percent = 1904967 id = 3809934, percent = 1904967 id = 3809934, percent = 1904967

    转载请注明原文地址: https://ju.6miu.com/read-149776.html

    最新回复(0)