boost構造和解析json

 構造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);
}


解析實例:

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);
}


發佈了74 篇原創文章 · 獲贊 58 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章