在C ++中创建一个json数组 [英] Create a json array in C++

查看:253
本文介绍了在C ++中创建一个json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So im trying to create a json Object in c++ dynamically. I want to add a timestamp and then an array with the data included.

这就是我的json字符串的样子:

So thats what my json String would look like :

{
    "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上找到了这个

Im totally new to C++ and im using the Casablanca ( C++ REST SDK) package. So im having a really hard time producing the code. And i cant find any working solutions. I found this on the 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"));

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

and that works for me. But how do i create an array?

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

i tried several things but nothing worked. Maybe theres a better package? But as far as i understood its an official micorosft package for json and http.

帮助真的很好!

推荐答案

有2种机制.如果您习惯于使用std c ++库,则应该看起来很熟悉.元素向量是从std :: vector派生的.

There are 2 mechanisms. If you are used to std c++ libraries, this should look familiar. Element vector is derived from 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);

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

And, if you prefer a cleaner look, and can accept a less efficient compiled result:

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

从您的信息中您尝试了很多东西.如果您尝试过类似的机制,但它们无法正常工作,请给我们提供一个示例程序,以消除故障,然后再进行破解.

From your message you have tried a bunch of stuff. If you have tried mechanisms like these but they didn't work, give us a sample program that demontrates the failure and we'll have a crack at it.

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

To get the basic structure in your file above:

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;

这篇关于在C ++中创建一个json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆