c++庫之jsoncpp庫

來源:互聯網
上載者:User

標籤:

        官方網址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庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.