Boost ptree array of numbers

蓝咒 提交于 2019-12-13 17:24:29

问题


I use the following code to create an array of the numbers.

After running the following code, I reveive the following results:

{
    "": "1.100000",
    "": "2.200000",
    "": "3.300000"
}

It is good except for my desired result has to be an array of numbers not string. Adding a number directly by boost::property_tree::ptree(x) gives me an error too. How can I produce my output json results?

{
    "": 1.100000,
    "": 2.200000,
    "": 3.300000
}

Code:

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

int main()
{
    boost::property_tree::ptree pt;
    std::vector<double> Vec={1.1,2.2,3.3};
    for(double x:Vec)
    {
        std::string x_string=std::to_string(x);
        pt.push_back(
            std::make_pair("", 
            boost::property_tree::ptree(x_string)) );

    }
    boost::property_tree::json_parser::write_json(std::cout, pt);
    std::cout<<std::endl;
    return 0;
}

回答1:


PTree has no such features.

Everything is text in the serialized formats. Even if the chosen backend format could support (limited) typed data.

Documentation proof:

As I keep re-stating:

Boost does not have an XML library.

Boost does not have a JSON library.

Boost has a Property Tree library. It deals with Property Trees. Not JSON, XML or whatever else.



来源:https://stackoverflow.com/questions/41609939/boost-ptree-array-of-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!