如何使用C ++ Boost解析JSON数组? [英] How can I parse JSON arrays with C++ Boost?

查看:228
本文介绍了如何使用C ++ Boost解析JSON数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中包含一些类似于以下内容的JSON内容:

I have a file containing some JSON content that looks like:

{
  "frame":
  {
    "id": "0",
    "points":
    [
      [ "0.883", "0.553", "0" ],
      [ "0.441", "0.889", "0" ],
    ]
  },
  "frame":
  ...
}

如何使用C ++和Boost ptree解析double数组的值?

How do I parse the values of the double array using C++ and Boost ptree?

推荐答案

使用迭代器Luke.

首先,您必须解析文件:

First , you have to parse the file:

boost::property_tree::ptree doc;
boost::property_tree::read_json("input_file.json", doc);

...现在,因为似乎您在顶级词典中有多个框架"键,所以您必须遍历它们:

... now, because it seems you have multiple "frame" keys in the top level dictionary you must iterate over them:

BOOST_FOREACH (boost::property_tree::ptree::value_type& framePair, doc) {
    // Now framePair.first == "frame" and framePair.second is the subtree frame dictionary
} 

遍历行和列是相同的:

BOOST_FOREACH (boost::property_tree::ptree::value_type& rowPair, frame.get_child("points")) {
    // rowPair.first == ""
    BOOST_FOREACH (boost::property_tree::ptree::value_type& itemPair, rowPair.second) {
        cout << itemPair.second.get_value<std::string>() << " ";
    }
    cout << endl;
}

我没有测试代码,但是这个想法可行:-)

I didn't test the code, but the idea will work :-)

这篇关于如何使用C ++ Boost解析JSON数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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