在 C 中创建一个 json 数组

新手上路,请多包涵

所以我试图在 C++ 中动态创建一个 json 对象。我想添加一个时间戳,然后添加一个包含数据的数组。

所以这就是我的 json String 的样子:

 {
    "timestep": "2160.00",
    "vehicles": [
        {
            "id": "35092_35092_353",
            "x": "6.988270",
            "y": "50.872139",
            "angle": "-20.812787",
            "type": "passenger_P_14_1",
            "speed": "0.000000",
            "pos": "4.600000",
            "lane": "4.600000",
            "slope": "4.600000"
        },
        {
            "id": "35092_35092_353",
            "x": "6.988270",
            "y": "50.872139",
            "angle": "-20.812787",
            "type": "passenger_P_14_1",
            "speed": "0.000000",
            "pos": "4.600000",
            "lane": "4.600000",
            "slope": "4.600000"
        },
        {
            "id": "35092_35092_353",
            "x": "6.988270",
            "y": "50.872139",
            "angle": "-20.812787",
            "type": "passenger_P_14_1",
            "speed": "0.000000",
            "pos": "4.600000",
            "lane": "4.600000",
            "slope": "4.600000"
        }
    ]
}

我对 C++ 完全陌生,我使用的是 Casablanca (C++ REST SDK) 包。所以我很难生成代码。而且我找不到任何可行的解决方案。我在 wiki 上找到了这个

创建一个 JSON 对象:

 json::value obj;
obj[L"key1"] = json::value::boolean(false);
obj[L"key2"] = json::value::number(44);
obj[L"key3"] = json::value::number(43.6);
obj[L"key4"] = json::value::string(U("str"));

这对我有用。但是我如何创建一个数组?

我尝试了几件事,但没有任何效果。也许有更好的包?但据我了解,它是 json 和 http 的官方 micorosft 包。

帮助会非常好!

原文由 Hannes 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

有2个机制。如果您习惯于 std c++ 库,这应该看起来很熟悉。元素向量派生自 std::vector。

 json::value::element_vector e;
// the first item in the pair is the array index, the second the value
e.push_back(std::make_pair(json::value(0), json::value(false)));
e.push_back(std::make_pair(json::value(1), json::value::string(U("hello"))));
json::value arr(e);

而且,如果您更喜欢更简洁的外观,并且可以接受效率较低的编译结果:

 json::value arr;
arr[0] = json::value(false);
arr[1] = json::value(U("hello"));

从您的消息中,您尝试了很多东西。如果您尝试过类似的机制,但它们不起作用,请给我们一个演示失败的示例程序,我们将对其进行破解。

要在上面的文件中获取基本结构:

 json::value vehicles;
vehicles[0] = // 1st vehicle object
vehicles[1] = // 2nd vehicle object
// etc
json::value root;
root[L"timestep"] = json::number(2160.0);
root[L"vehicles"] = vehicles;

原文由 FatalFlaw 发布,翻译遵循 CC BY-SA 3.0 许可协议

以下示例 json C++ 字符串数组适用于我。

 const char * const line_items_items =
"[\
    {\
        \"commodity_code\": \"44121903\",\
        \"description\": \"Miscellaneous goods\",\
        \"upc\": \"65100004327\",\
        \"quantity\": \"2\",\
        \"unit_of_measurement\": \"M62\",\
        \"unit_cost\": \"23.09\",\
        \"discount_amount\": \"10.03\",\
        \"total_amount\": \"50.03\",\
        \"tax_amount\": \"10.05\",\
        \"extended_amount\": \"76.04\",\
        \"debit_or_credit_indicator\": \"credit\",\
        \"net_or_gross_indicator\": \"net\"\
    },\
    {\
        \"commodity_code\": \"44121809\",\
        \"description\": \"Miscellaneous goods\",\
        \"upc\": \"65100007654\",\
        \"quantity\": \"4\",\
        \"unit_of_measurement\": \"M66\",\
        \"unit_cost\": \"35.09\",\
        \"discount_amount\": \"5.06\",\
        \"total_amount\": \"0.53\",\
        \"tax_amount\": \"8.07\",\
        \"extended_amount\": \"96.12\",\
        \"debit_or_credit_indicator\": \"debit\",\
        \"net_or_gross_indicator\": \"gross\"\
    }\
]";

原文由 Pabitra Dash 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题