c++库之jsoncpp库

        官方网址https://github.com/open-source-parsers/jsoncpp。

         我是以源码的形式使用的,从官网下载源码后解压,然后copy相应的.h和.cpp文件到你的项目目录。编译使用即可。用起来也非常简单,主要以数组的形式进行

使用。下面从自己的项目中复制出一段代码作为示例:

json数据:

{
    "msg": "",
    "status": "1",
    "message": {
        "yuyue": [
            {
                "id": "1776",
                "tid": "2",
                "userid": "77878",
                "yuyuetime": "2015-05-07",
                "catid": "5",
                "create_time": "1430130036",
                "type": "1",
                "contact": "13501621744",
                "sjd": "16:00-17:00",
                "from": "1",
                "title": "亲子关系",
                "username": "测试帐号",
                "padcode": null,
                "iptvcode": "021000008003"
            },
            {
                "id": "1539",
                "tid": "2",
                "userid": "33990",
                "yuyuetime": "2015-04-16",
                "catid": "746",
                "create_time": "1428458444",
                "type": "0",
                "contact": "50691881/13917894758",
                "sjd": "09:00-10:00",
                "from": "0",
                "title": null,
                "username": "sfsfs",
                "padcode": null,
                "iptvcode": "021000008004"
            }
        ]
    }
}

解析代码:

void loadDocJson()
{
    // 解析json用Json::Reader  
    Json::Reader reader;    
    std::ifstream file;  
    file.open ("doctorylist.json");

    Json::Value root;
    Json::Value item;
    reader.parse(file,root);

    int size = root["message"]["yuyue"].size();

    DoctoryInfo dcinfo;

    for (int i=0; i<size; ++i)
    {
        QString id=QString::fromStdString(root["message"]["yuyue"][i]["id"].asString());
        strcpy(dcinfo.id,id.toStdString().c_str());

        //QString userid=QString::fromStdString(root["message"]["yuyue"][i]["userid"].asString());
        //strcpy(dcinfo.userid,userid.toStdString().c_str());

        QString username=QString::fromStdString(root["message"]["yuyue"][i]["username"].asString());
        strcpy(dcinfo.username,username.toStdString().c_str());

        QString titleclass=QString::fromStdString(root["message"]["yuyue"][i]["title"].asString());
        strcpy(dcinfo.titleclass,titleclass.toStdString().c_str());


        QString yuyuetime=QString::fromStdString(root["message"]["yuyue"][i]["yuyuetime"].asString());
        strcpy(dcinfo.yuyuetime,yuyuetime.toStdString().c_str());

        QString create_time=QString::fromStdString(root["message"]["yuyue"][i]["create_time"].asString());
        strcpy(dcinfo.create_time,create_time.toStdString().c_str());

        QString iptvcode=QString::fromStdString(root["message"]["yuyue"][i]["iptvcode"].asString());
        strcpy(dcinfo.iptvcode,iptvcode.toStdString().c_str());

        map_docinfo.insert(dcinfo.id,dcinfo);

        //qDebug()<<dcinfo.userid<<"xxxxxxxx";
    }

}

       另外值得指出 的一点是,对于中文,json网络传输一般采用unicode编码,解析的时候往往会出现乱码,其实这个jsoncpp库也做 了完美的解决,但需要修改下源码。

打开文件json_reader.cpp,找到codePointToUTF8函数,添加如下代码:

   else if (cp <= 0x7FF)
   {
      result.resize(2);
      result[1] = static_cast<char>(0x80 | (0x3f & cp));
      result[0] = static_cast<char>(0xC0 | (0x1f & (cp >> 6)));
   }
   //byliu add -begin
   else if((cp >= 0x4E00 && cp <= 0X9FA5)||(cp >= 0xF900 && cp<= 0xFA2D))
   {
       wchar_t src[2]={0};
       char dest[5]={0};
       src[0]=static_cast<wchar_t>(cp);
       std::string curLocale=setlocale(LC_ALL,NULL);
       setlocale(LC_ALL,"chs");
       wcstombs_s(NULL,dest,5,src,2);
       result=dest;
       setlocale(LC_ALL,curLocale.c_str());
   }
   //beliu add -end

   当然也可把jsoncpp编译成lib库来调用。我是在qt里面使用json的,qt自带的库没研究,但个人感觉这个c++库蛮好用的。另外关于xml,c++也有一个库,好像叫markup,也

非常好用,个人感觉比qt自带的那个库好用多了。在另外一篇文章里做介绍。


你可能感兴趣的:(c++库之jsoncpp库)