【问题标题】:How can I parse nested arrays?如何解析嵌套数组?
【发布时间】:2019-01-21 23:59:47
【问题描述】:

我的 JSON 格式如下:

  "results": [
    {
      "plate": "FRJ7248",
      "confidence": 94.583724975586,
      "matches_template": 1,
      "plate_index": 0,
      "region": "us-ny",
      "region_confidence": 88,
      "processing_time_ms": 94.328330993652,
      "requested_topn": 25,
      "coordinates": [
        {
          "x": 1545,
          "y": 233
        },
        {
          "x": 1640,
          "y": 241
        },
...
...

我想知道如何解析 coordinates 数组,我尝试了以下方法:

BOOST_FOREACH(boost::property_tree::ptree::value_type& v, pt.get_child("results")) {
    ptree subtree1 = v.second.get<string>;
    BOOST_FOREACH(boost::property_tree::ptree::value_type& w, subtree1) {
        LOG_NS_INFO << "OpenALPR     X  " << w.second.get<string>("x");
        LOG_NS_INFO << "OpenALPR     Y  " << w.second.get<string>("y");
    }
}

但是得到了

error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘boost::property_tree::ptree {aka boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >}’ requested
             ptree subtree1 = v.second.get<string>;

我该如何解决这个问题?

【问题讨论】:

  • 我不熟悉boost,但应该是v.second.get&lt;string&gt;()吧?
  • 正如错误消息提示的那样,您忘记致电get。我的第一个猜测是你应该写v.second.get_child("coordinates")
  • @molbdnilo 您可以将其移至答案中,我会接受!我需要ptree subtree1 = v.second.get_child("coordinates");

标签: c++ json parsing boost nested


【解决方案1】:

消息显示"conversion from ‘&lt;unresolved overloaded function type&gt;’",因为您没有致电get

但是你不应该使用get,你应该做和"results"一样的事情:

ptree subtree1 = v.second.get_child("coordinates"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2014-08-08
    相关资源
    最近更新 更多