我需要创建我的 .json 数组,如下所示:
{
"airports": [{
"address": "Horley, Gatwick RH6 0NP, UK",
"city": "London",
"shortVersion": "LGW"
},
{
"address": "Marupe, LV-1053",
"city": "Riga",
"shortVersion": "RIX"
}
]
}
但我现在看起来像这样:
{
"airports": {
"(LGW)": {
"address": "Horley, Gatwick RH6 0NP, UK",
"city": "London",
"shortVersion": "(LGW)"
},
"(RIX)": {
"address": "Marupe, LV-1053",
"city": "Riga",
"shortVersion": "(RIX)"
}
}
}
我现在用于用户输入的代码是这样的:
airports["airports"][inputShortVersion]["shortVersion"] = inputShortVersion;
airports["airports"][inputShortVersion]["city"] = inputCity;
airports["airports"][inputShortVersion]["address"] = inputAddress;
我已经搜索了一整天关于如何做到这一点,但我最接近的是它确实创建了上述数组但在输入后它会覆盖最后一个机场数据。
我正在使用 nlohmann json 库。