boost构造,解析json

boost构造,解析json
void asynDBCenter::isGetActorInfoEx(void* on_process, const char* arg)
{
    std::stringstream ros(arg);
    boost::property_tree::ptree pt;
    boost::property_tree::read_json(ros, pt);
    int actorID = pt.get<int>("actorID");

    ActorInfoEx info={0};
    bool ret = m_dbcenter->getActorInfoEx(actorID, &info);
     
    std::stringstream wos;
    boost::property_tree::ptree root,child_eq, child_kep;
    root.put<bool>("ret", ret);
    if(ret)
    {
        root.put<int>("_id", info.actor.id);
        root.put<std::string>("name", info.actor.name);
        root.put<int>("sex", info.actor.sex);
        root.put<int>("prof", info.actor.prof);
        root.put<int>("uid", info.uid);
        root.put<int>("gold", info.gold);
        root.put<int>("map", info.map);
        root.put<int>("cur_hp", info.stat.cur_hp);
        root.put<int>("max_hp", info.stat.max_hp);
        root.put<int>("cur_mp", info.stat.cur_mp);
        root.put<int>("max_mp", info.stat.max_mp);
        root.put<int>("x", info.pos.x);
        root.put<int>("y", info.pos.y);
        root.put<int>("level", info.level);
        root.put<int>("neq", info.neq);
     //插入"":{"":"",
     //    "":""}
     //形式的json
for(int i = 0; i < info.neq; ++i) { char str[10] = {0}; sprintf(str, "epos%02d", i); child_eq.put<int>(str, info.eqs[i]); } root.put_child("eqs", child_eq);
   //插入"":[
    //    {"":""},
    //    {"",""}
    //  ]数组类型的文档
    
for (auto kk:info.vctKk) { boost::property_tree::ptree child; child.put<int>("id", kk.eid); child.put<int>("pos_x", kk.epos.x); child.put<int>("pos_y", kk.epos.y); child_kep.push_back(std::make_pair("", child)); } root.put_child("keqs",child_kep); } boost::property_tree::write_json(wos, root); HrPkt pkt; pkt.hr = wos.str(); pkt.on_process = on_process; pkt.process = std::bind(&asynDBCenter::onGetActorInfoEx, this, std::placeholders::_1, std::placeholders::_2); push_hr(pkt); }
boost构造,解析json

 针对上面构造的进行解析

boost构造,解析json
void asynDBCenter::onGetActorInfoEx(void* on_process, const char* arg)
{
    std::stringstream os(arg);
    boost::property_tree::ptree root;
    boost::property_tree::read_json(os, root);
    ActorInfoEx actEx={0};
    bool ret = root.get<bool>("ret");
    if(ret)
    {
        actEx.actor.id = root.get<int>("_id");
        strcpy(actEx.actor.name, root.get<std::string>("name").c_str());
        actEx.actor.sex = (DB::sex)root.get<int>("sex");
        actEx.actor.prof = (RoleProf)root.get<int>("prof");
        actEx.uid = root.get<int>("uid");
        actEx.gold = root.get<int>("gold");
        actEx.map = root.get<int>("map");
        actEx.stat.cur_hp = root.get<int>("cur_hp");
        actEx.stat.max_hp = root.get<int>("max_hp");
        actEx.stat.cur_mp = root.get<int>("cur_hp");
        actEx.stat.max_mp = root.get<int>("max_mp");
        actEx.pos.x = root.get<int>("x");
        actEx.pos.y = root.get<int>("y");
        actEx.level = root.get<int>("level");
        actEx.neq = root.get<int>("neq");
        auto child = root.get_child("eqs");
        int i = 0;
     //遍历里面的记录
for(auto it : child) { actEx.eqs[i++] = it.second.get_value<int>(); if(i >= MAX_EQ_NUMB) break; } auto arr = root.get_child("keqs");
    //遍历数组中的文档
for (auto it:arr) { KnapsackEq kk = {0}; kk.eid = it.second.get<int>("id"); kk.epos.x = it.second.get<int>("pos_x"); kk.epos.y = it.second.get<int>("pos_y"); actEx.vctKk.push_back(kk); } } auto on_getActorInfoEx = *(std::function<void(bool, ActorInfoEx) >*)on_process; on_getActorInfoEx(ret, actEx); }
boost构造,解析json

 boost::property_tree::ptree感觉就像是一个树,每个节点有key,value,不是二叉树,value又可以指向下面的节点。

boost构造,解析json,布布扣,bubuko.com

boost构造,解析json

上一篇:jquery异步调用页面后台方法


下一篇:PHP 变量