首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用助推阅读json

用助推阅读json
EN

Stack Overflow用户
提问于 2018-08-14 13:37:37
回答 1查看 77关注 0票数 0

我希望使用boost从JSON字符串中读取数据,这些数据看起来可能如下所示:

代码语言:javascript
复制
    {
  "version": 2,
  "data_type": "alpr_results",
  "epoch_time": 1534253281728,
  "img_width": 1920,
  "img_height": 1080,
  "processing_time_ms": 271.27728271484,
  "uuid": "",
  "error": false,
  "regions_of_interest": [
    {
      "x": 0,
      "y": 0,
      "width": 1920,
      "height": 1080
    }
  ],
  "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
        },
        {
          "x": 1635,
          "y": 284
        },
        {
          "x": 1543,
          "y": 276
        }
      ],
      "vehicle_region": {
        "x": 1388,
        "y": 0,
        "width": 403,
        "height": 403
      },
      "candidates": [
        {
          "plate": "FRJ7248",
          "confidence": 94.583724975586,
          "matches_template": 1
        },
        {
          "plate": "FRJ724",
          "confidence": 81.629249572754,
          "matches_template": 1
        },
        {
          "plate": "FR7248",
          "confidence": 81.344184875488,
          "matches_template": 0
        },
        {
          "plate": "FJ7248",
          "confidence": 81.199394226074,
          "matches_template": 0
        },
        {
          "plate": "RJ7248",
          "confidence": 81.19913482666,
          "matches_template": 0
        },
        {
          "plate": "FRJ7246",
          "confidence": 81.167663574219,
          "matches_template": 1
        },
        {
          "plate": "FRJ7240",
          "confidence": 81.13761138916,
          "matches_template": 1
        },
        {
          "plate": "FRJ7243",
          "confidence": 81.127967834473,
          "matches_template": 1
        },
        {
          "plate": "FRJ7245",
          "confidence": 81.124664306641,
          "matches_template": 1
        },
        {
          "plate": "FRU7248",
          "confidence": 81.089027404785,
          "matches_template": 1
        },
        {
          "plate": "FBJ7248",
          "confidence": 81.082328796387,
          "matches_template": 1
        },
        {
          "plate": "FRD7248",
          "confidence": 81.080703735352,
          "matches_template": 1
        },
        {
          "plate": "ERJ7248",
          "confidence": 81.068466186523,
          "matches_template": 1
        },
        {
          "plate": "FR724",
          "confidence": 68.389724731445,
          "matches_template": 0
        },
        {
          "plate": "FJ724",
          "confidence": 68.244926452637,
          "matches_template": 0
        },
        {
          "plate": "RJ724",
          "confidence": 68.244659423828,
          "matches_template": 0
        },
        {
          "plate": "FRU724",
          "confidence": 68.134559631348,
          "matches_template": 1
        },
        {
          "plate": "FBJ724",
          "confidence": 68.127861022949,
          "matches_template": 1
        },
        {
          "plate": "FRD724",
          "confidence": 68.126243591309,
          "matches_template": 1
        },
        {
          "plate": "ERJ724",
          "confidence": 68.113998413086,
          "matches_template": 1
        },
        {
          "plate": "F7248",
          "confidence": 67.959869384766,
          "matches_template": 0
        },
        {
          "plate": "R7248",
          "confidence": 67.959594726562,
          "matches_template": 0
        },
        {
          "plate": "FR7246",
          "confidence": 67.92813873291,
          "matches_template": 0
        },
        {
          "plate": "FR7240",
          "confidence": 67.898078918457,
          "matches_template": 0
        },
        {
          "plate": "FR7243",
          "confidence": 67.888442993164,
          "matches_template": 0
        }
      ]
    },
    {
      "plate": "T701486C",
      "confidence": 93.531463623047,
      "matches_template": 0,
      "plate_index": 1,
      "region": "us-ny",
      "region_confidence": 84,
      "processing_time_ms": 94.328330993652,
      "requested_topn": 25,
      "coordinates": [
        {
          "x": 1005,
          "y": 407
        },
        {
          "x": 1101,
          "y": 400
        },
        {
          "x": 1102,
          "y": 454
        },
        {
          "x": 1007,
          "y": 462
        }
      ],
      "vehicle_region": {
        "x": 782,
        "y": 69,
        "width": 543,
        "height": 543
      },
      "candidates": [
        {
          "plate": "T701486C",
          "confidence": 93.531463623047,
          "matches_template": 0
        },
        {
          "plate": "T70T486C",
          "confidence": 82.387817382812,
          "matches_template": 0
        },
        {
          "plate": "T70486C",
          "confidence": 82.198287963867,
          "matches_template": 0
        },
        {
          "plate": "T70I486C",
          "confidence": 82.151023864746,
          "matches_template": 0
        },
        {
          "plate": "701486C",
          "confidence": 82.046699523926,
          "matches_template": 0
        },
        {
          "plate": "T70146C",
          "confidence": 81.921669006348,
          "matches_template": 0
        },
        {
          "plate": "7701486C",
          "confidence": 81.844429016113,
          "matches_template": 0
        },
        {
          "plate": "1701486C",
          "confidence": 81.822814941406,
          "matches_template": 0
        },
        {
          "plate": "T701466C",
          "confidence": 81.808143615723,
          "matches_template": 0
        },
        {
          "plate": "70T486C",
          "confidence": 70.903053283691,
          "matches_template": 0
        },
        {
          "plate": "T70T46C",
          "confidence": 70.778022766113,
          "matches_template": 0
        },
        {
          "plate": "70486C",
          "confidence": 70.713523864746,
          "matches_template": 0
        },
        {
          "plate": "770T486C",
          "confidence": 70.700782775879,
          "matches_template": 0
        },
        {
          "plate": "170T486C",
          "confidence": 70.679168701172,
          "matches_template": 0
        },
        {
          "plate": "70I486C",
          "confidence": 70.666259765625,
          "matches_template": 0
        },
        {
          "plate": "T70T466C",
          "confidence": 70.664497375488,
          "matches_template": 0
        },
        {
          "plate": "T7046C",
          "confidence": 70.588493347168,
          "matches_template": 0
        },
        {
          "plate": "T70I46C",
          "confidence": 70.541229248047,
          "matches_template": 0
        },
        {
          "plate": "770486C",
          "confidence": 70.511253356934,
          "matches_template": 0
        },
        {
          "plate": "170486C",
          "confidence": 70.489639282227,
          "matches_template": 0
        },
        {
          "plate": "T70466C",
          "confidence": 70.474967956543,
          "matches_template": 0
        },
        {
          "plate": "770I486C",
          "confidence": 70.463996887207,
          "matches_template": 0
        },
        {
          "plate": "170I486C",
          "confidence": 70.442375183105,
          "matches_template": 0
        },
        {
          "plate": "70146C",
          "confidence": 70.436904907227,
          "matches_template": 0
        },
        {
          "plate": "T70I466C",
          "confidence": 70.427703857422,
          "matches_template": 0
        }
      ]
    }
  ]
}

我现在感兴趣的是"plate" in "results",即FRJ7248T701486C。到目前为止,我已经知道了:

代码语言:javascript
复制
int alpr_json_decode(std::string json)
{
    try 
    {   
        std::stringstream ss; 
        ss << json;

        boost::property_tree::ptree pt; 
        boost::property_tree::read_json(ss,pt);

        BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("results.plate")) {
            assert(v.first.empty());
            std::cout << v.second.data() << std::endl;
        }   
    }   
    catch(std::exception const& e)
    {   
        std::cerr <<e.what() << std::endl;
    }   
    return -1; 
}

我得到了No such node (results.plate)。为什么,我在这里做错什么了?

我也尝试过.get_child("results"),它什么也不返回。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-14 16:28:44

结果是对象数组,所以根据参考文献

JSON数组映射到节点。每个元素都是一个名为空的子节点。

你的json被翻译成

代码语言:javascript
复制
results
{
   ""       
   {
     plate FRJ7248      
     ...
   }   
   ""
   {
    plate T701486C
   }
}

您应该通过pt.get_child("results")读取列表中的所有对象,然后对每个子树调用v.second.get<string>("XXX")来读取对象的成员。

测试代码:

代码语言:javascript
复制
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("results")) {
    cout << v.second.get<string>("plate") << endl;
    cout << v.second.get<string>("confidence") << endl;
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51842839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档