boost.property_tree读取json预期的输入结束

时间:2015-05-16 19:13:39

标签: c++ json boost

我正在使用boost来读取JSON文件 这是我的代码

for (int i = 0; i < 10; i++)
{
    try
    {
        boost::property_tree::ptree pt;
        boost::property_tree::read_json("macAddr.txt", pt);

        BOOST_FOREACH(boost::property_tree::ptree::value_type& v, pt)
        {
            BOOST_FOREACH(boost::property_tree::ptree::value_type& i, v.second)
            {
                std::string s = i.second.get_value("");
                std::cerr << s << "\n";
            }
            std::cerr << "\n\n";
        }
    }
    catch (std::exception const& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

并抛出异常“macAddr.txt(1):输入的预期结束”

JSON文件的内容是:

[{"mac":"60:21:C0:20:6A:8C","power":-73,"sensor_id":254,"timestamp":"2015-05-16 19:32:19"},{"mac":"CC:FA:00:B4:3D:44","power":-52,"sensor_id":254,"timestamp":"2015-05-16 19:32:17"},{"mac":"CC:3A:61:82:D1:20","power":-76,"sensor_id":254,"timestamp":"2015-05-16 19:32:12"},{"mac":"0C:48:85:FF:1C:2D","power":-79,"sensor_id":254,"timestamp":"2015-05-16 19:32:12"},{"mac":"34:A3:95:A2:00:2E","power":-76,"sensor_id":254,"timestamp":"2015-05-16 19:32:11"}]

1 个答案:

答案 0 :(得分:3)

您可能有跟踪数据,例如

{ "a" : 1 }
{ "b" : 2 }

属性树只会读取第一个顶层对象或数组。

<强>更新

MVCE不是问题: Live On Coliru

使用尾随字词,您会看到您报告的错误: Live On Coliru