gpt4 book ai didi

c++ - 如何制作 jsoncpp 数组?

转载 作者:搜寻专家 更新时间:2023-10-31 00:53:32 25 4
gpt4 key购买 nike

我坚持 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";

编辑:
我想在事件中打印所有内容。
我不想单独打印电线。

最佳答案

您需要使用 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);
}

关于c++ - 如何制作 jsoncpp 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48378991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com