C++使用JsonCpp解析Json数据 示例

 1、拼接json

  std::vector<TAccelKeyInfo>::const_iterator iter = vec_keyinfo.begin();
  //for (; iter != vec_keyinfo.end(); ++iter) {
  Json::FastWriter writer;
  Json::Value value;
  value["shortcuts"]["order"] = iter->order_;
  value["shortcuts"]["description_id"] = iter->description_id_;
  value["shortcuts"]["global"] = iter->global_;
  value["shortcuts"]["can_edit"] = iter->can_edit_;
  value["shortcuts"]["accel"]["fVirt"] = iter->accel_.fVirt;
  value["shortcuts"]["accel"]["key"] = iter->accel_.key;
  value["shortcuts"]["accel"]["cmd"] = iter->accel_.cmd;

  std::string output_data = writer.write(value);
  //}
  // output_data = 
/*
{
    "shortcuts": {
        "accel": {
            "cmd": 49253,
            "fVirt": 1,
            "key": 117
        },
        "can_edit": true,
        "description_id": 15027,
        "global": false,
        "order": 0
    }
}
*/

 

2、解析json到数据

/*
{
    "shortcuts": [
        {
            "order": 0,
            "description_id": 15001,
            "global": false,
            "can_edit": true,
            "accel": {
                "fVirt": 100,
                "key": 10,
                "cmd": 10001
            }
        },
        {
            "order": 1,
            "description_id": 15002,
            "global": true,
            "can_edit": false,
            "accel": {
                "fVirt": 101,
                "key": 20,
                "cmd": 10002
            }
        },
        {
            "order": 2,
            "description_id": 15003,
            "global": true,
            "can_edit": true,
            "accel": {
                "fVirt": 103,
                "key": 30,
                "cmd": 10003
            }
        }
    ]
}
*/
Json::Value value;
  Json::Reader reader;
  std::ifstream stream;
  stream.open ("C:\\Users\\canhui.wang\\Desktop\\shortcuts.json", std::ios::binary );

  if(!reader.parse(stream, value)) return vec_temp;
// 针对json数组,方法1
  const Json::Value shortcuts = value["shortcuts"];
  for ( unsigned int index = 0; index < shortcuts.size(); ++index ) {
    int order = shortcuts[index]["order"].asInt();
    int description_id = shortcuts[index]["description_id"].asInt();
    bool global = shortcuts[index]["global"].asBool();
    bool can_edit = shortcuts[index]["can_edit"].asBool();
    int fVirt = shortcuts[index]["accel"]["fVirt"].asInt();
    int key = shortcuts[index]["accel"]["key"].asInt();
    int cmd = shortcuts[index]["accel"]["cmd"].asInt();
    std::wostringstream wss;
    wss << L"value[\"shortcuts\"] order =" << order << L" description_id= " << description_id << L" global= " << global << L" can_edit =" << can_edit << L" fVirt= " << fVirt << L" key= " << key << L" cmd= " << cmd << L"\n";
    ::OutputDebugString(wss.str().c_str());
  }

  // 针对json数组,方法2
  for (Json::ValueIterator iter = value["shortcuts"].begin(); iter != value["shortcuts"].end(); iter++) {
    int order = (*iter)["order"].asInt();
    int description_id = (*iter)["description_id"].asInt();
    bool global = (*iter)["global"].asBool();
    bool can_edit = (*iter)["can_edit"].asBool();
    int fVirt = (*iter)["accel"]["fVirt"].asInt();
    int key = (*iter)["accel"]["key"].asInt();
    int cmd = (*iter)["accel"]["cmd"].asInt();

    std::wostringstream wss;
    wss << L"ValueIterator order =" << order << L" description_id= " << description_id << L" global= " << global << L" can_edit =" << can_edit << L" fVirt= " << fVirt << L" key= " << key << L" cmd= " << cmd << L"\n";
    ::OutputDebugString(wss.str().c_str());

  }

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值