如何制作 jsoncpp 数组?
How do I make a jsoncpp array?
我受困于 jsoncpp。我想创建一个这样的数组:
"Cords": [{"x": 10, "y": 20}, {"x": 70, "y": 40}, {"x": 15, "y": 65}]
我设法用 jsoncpp 做了常规的事情(见下文),但我被困在制作 JSON 数组的这种情况下。
Json::Value event;
event["name"] = "Joe";
event["Direction"]["left"]["x"] = "1338";
event["Direction"]["right"]["x"] = "1337";
编辑:
我想在活动中全部打印出来。
我不想单独打印电线。
可能是这样的
Json::Value min;
Json::Value event;
event["x"] = 10;
event["y"] = 20;
min["Cords"] = event;
// Output to see the result
cout<<min.toStyledString()
您需要使用 operator[]
的 int
重载来定义数组
Json::Value coord(int x, int y)
{
Json::Value result;
result["x"] = x;
result["y"] = y;
return result;
}
void make_event(Json::Value & event)
{
Json::Value & coords = event["Cords"];
coords[0] = coord(10, 20);
coords[1] = coord(70, 40);
coords[2] = coord(15, 65);
}
我受困于 jsoncpp。我想创建一个这样的数组:
"Cords": [{"x": 10, "y": 20}, {"x": 70, "y": 40}, {"x": 15, "y": 65}]
我设法用 jsoncpp 做了常规的事情(见下文),但我被困在制作 JSON 数组的这种情况下。
Json::Value event;
event["name"] = "Joe";
event["Direction"]["left"]["x"] = "1338";
event["Direction"]["right"]["x"] = "1337";
编辑:
我想在活动中全部打印出来。
我不想单独打印电线。
可能是这样的
Json::Value min;
Json::Value event;
event["x"] = 10;
event["y"] = 20;
min["Cords"] = event;
// Output to see the result
cout<<min.toStyledString()
您需要使用 operator[]
的 int
重载来定义数组
Json::Value coord(int x, int y)
{
Json::Value result;
result["x"] = x;
result["y"] = y;
return result;
}
void make_event(Json::Value & event)
{
Json::Value & coords = event["Cords"];
coords[0] = coord(10, 20);
coords[1] = coord(70, 40);
coords[2] = coord(15, 65);
}