c++代码- rapidjson解析json

参考:

http://www.jyguagua.com/?p=2469

https://www.cnblogs.com/sea-stream/p/11105387.html 

{
    "result": [
        {
            "tagtype": "nut_of_sand",
            "data": [
                837.0,
                632.0,
                85.0,
                86.0
            ],
            "comment": ""
        },
        {
            "tagtype": "nut_of_sand",
            "data": [
                593.0,
                769.0,
                75.0,
                85.0
            ],
            "comment": ""
        },
        {
            "tagtype": "nut_of_sand",
            "data": [
                730.0,
                1078.0,
                72.0,
                81.0
            ],
            "comment": ""
        },
        {
            "tagtype": "nut_of_sand",
            "data": [
                983.0,
                950.0,
                83.0,
                88.0
            ],
            "comment": ""
        },
        {
            "tagtype": "nut_of_sand",
            "data": [
                1246.0,
                830.0,
                79.0,
                79.0
            ],
            "comment": ""
        },
        {
            "tagtype": "nut_of_sand",
            "data": [
                1100.0,
                512.0,
                78.0,
                76.0
            ],
            "comment": ""
        },
        {
            "tagtype": "screw_nut_pin",
            "data": [
                875.0,
                201.0,
                153.0,
                154.0
            ],
            "comment": ""
        },
        {
            "tagtype": "screw_nut_pin",
            "data": [
                1078.0,
                185.0,
                154.0,
                153.0
            ],
            "comment": ""
        },
        {
            "tagtype": "cotter_pin",
            "data": [
                871.0,
                191.0,
                136.0,
                154.0
            ],
            "comment": ""
        },
        {
            "tagtype": "cotter_pin",
            "data": [
                1077.0,
                161.0,
                139.0,
                169.0
            ],
            "comment": ""
        },
        {
            "tagtype": "fastener3",
            "data": [
                253.1428571428571,
                1140.4285714285713,
                125.0,
                141.07142857142844
            ],
            "comment": ""
        },
        {
            "tagtype": "fastener4",
            "data": [
                1333.4999999999998,
                399.3571428571428,
                173.21428571428578,
                178.57142857142856
            ],
            "comment": ""
        },
        {
            "tagtype": "@1",
            "data": [
                81.03174603174602,
                873.7777777777778,
                449.20634920634916,
                633.3333333333333
            ],
            "comment": ""
        },
        {
            "tagtype": "@2",
            "data": [
                539.7619047619047,
                119.80952380952381,
                992.0634920634922,
                1050.7936507936508
            ],
            "comment": ""
        }
    ]
}

 

//json读取
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"
#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/error/en.h"
using namespace rapidjson;

void parse_standard_json(const char *jsonstr, vector<vector<int> > &bbox_result){

    Document d;
    if(d.Parse(jsonstr).HasParseError()){
        throw string("parse error!\n");
    }
    if(!d.IsObject()){
        throw string("should be an object!\n");
    }
    if(!d.HasMember("result")){
        throw string("member-'result' no found!");
    }

    if(!d.HasMember("result")){
        throw string("member-'result' no found!");
    }

    const Value &infoArray = d["result"];
    if(!infoArray.IsArray()){
        throw string("member-'result' error!");
    }

    for (int i = 0; i < infoArray.Size(); i++) {
        const Value& object = infoArray[i];
        std::string tagtype = object["tagtype"].GetString();
        //暂时只考虑nut
        string::size_type idx;
        idx = tagtype.find("nut");
        //不存在
        if(idx == string::npos)
        {
            continue;
        }
        else
        {
            const Value& data_object = object["data"];
            if (data_object.IsArray()) {
                vector<int> temp_bbox(4, 0);
                for (int j = 0; j < data_object.Size(); j++) {
                    double bbox = data_object[j].GetDouble();
                    temp_bbox[j] = (int)bbox;
                }
                bbox_result.push_back(temp_bbox);
            }
        }

    }
}

string readfile(const char *filename){
    FILE *fp = fopen(filename, "rb");
    if(!fp){
        std::cout << "open failed! file: " << filename << std::endl;
        return "";
    }

    char *buf = new char[1024*16];
    int n = fread(buf, 1, 1024*16, fp);
    fclose(fp);

    string result;
    if(n>=0){
        result.append(buf, 0, n);
    }
    delete []buf;
    return result;
}

//读取json
string jsonstr = readfile(standard_bboxjson_path.c_str());
vector<vector<int> > bbox_json;
parse_standard_json(jsonstr.c_str(), bbox_json);



 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值