Json解析數組執行個體

來源:互聯網
上載者:User

Json解析數組執行個體

1. JsonCPP簡介

jsoncpp是c++解析JSON串常用的解析庫之一。其常用的類有:

a) Json::Value 可以表示裡所有的類型,比如int,string,object,array等,其支援的類型可以參考Json:ValueType中的值。

b) Json::Reader 將json檔案流或字串解析到Json::Value,主要函數有Parse。

c) Json::Writer 與Json::Reader相反,將Json::Value轉化成字串流,注意它的兩個子類:Json::FastWriter和Json::StyleWriter,分別輸出不帶格式的json和帶格式的json。

d) Json::Value::Members 主要用於以STL風格解析JSON數組。看過原始碼的人已知道,Members其實是typedefvector而已。

2. JSONCPP解析樣本

a) 解析JSON串格式

{
"JsonID": "BD6D7FDA-54D2-468b-A3DE-9D5FBDB78207",
"Send": {
"ID": "B8E09E97-F379-4bb0-814A-389FD3F66631",
"Items": [
{
"Count": 2,
"Code": "0101",
"X": 1,
"Y": 2
},
{
"Count": 2,
"Code": "0101",
"X": 1,
"Y": 2
}
]
}
}

b) 產生的JSON串

{
"Ack": [
{
"ActualCount": 2,
"Code": "0101"
},
{
"ActualCount": 2,
"Code": "0101"
}
],
"JsonID": "BD6D7FDA-54D2-468b-A3DE-9D5FBDB78207"
}

c) 解析、產生JSON代碼

需要引入的.h檔案

[html] view plaincopy
  1. #pragma once
  2. #pragma comment(lib, "json_vc71_libmtd.lib")
  3. #include "json/json.h"

    實現

    [cpp] view plaincopy
    1. void CJSONTestDlg::OnBnClickedButton1()
    2. {
    3. CEdit* pEdit =(CEdit*)GetDlgItem(IDC_EDIT1);
    4. CString str;
    5. pEdit->GetWindowText(str); //str即為a)中定義的JSON串
    6. pEdit->FmtLines(true);
    7. Json::Reader reader;
    8. Json::Value root;
    9. if (reader.parse(WC2UT(str.GetBuffer(0)), root)) // reader將Json字串解析到root,root將包含Json裡所有子項目
    10. {
    11. std::string JsonID = root["JsonID"].asString();
    12. Json::Value rtnRoot;
    13. rtnRoot["JsonID"]=JsonID;
    14. Json::Value ack;
    15. Json::Value send = root["Send"];
    16. if(!send["Items"].isNull()){
    17. Json::Value Items = send["Items"];
    18. int sendSize = Items.size();
    19. for(int i=0;i std::string Code = Items[i]["Code"].asString();
    20. int x = Items[i]["X"].asInt();
    21. int y = Items[i]["Y"].asInt();
    22. int count = Items[i]["Count"].asInt();
    23. //更具讀到的JSON串中的值產生所需內容
    24. Json::Value newAckItem;
    25. newAckItem["Code"]=Code;
    26. newAckItem["ActualCount"]=count;
    27. ack.append(newAckItem);
    28. }
    29. }
    30. rtnRoot["Ack"]=ack;
    31. std::string rtnOut = rtnRoot.toStyledString();//產生帶格式的JSON串
    32. #ifdef UNICODE
    33. std::wstring stemp = s2ws(rtnOut);
    34. LPCWSTR result = stemp.c_str();
    35. #else
    36. LPCWSTR result = rtnOut.c_str();
    37. #endif
    38. MessageBox(result,_T("根據JSON串,產生的對應JSON串資訊"));
    39. CEdit* pEdit =(CEdit*)GetDlgItem(IDC_EDIT2);
    40. pEdit->SetWindowText(result);
    41. }else{
    42. CEdit* pRtnEdit =(CEdit*)GetDlgItem(IDC_EDIT2);
    43. pRtnEdit->SetWindowText(_T("JSON格式錯誤"));
    44. }
    45. }

      將JSONCPP以靜態庫方式匯入,需要注意項目中的代碼產生中的運行庫,和JSONCPP的靜態庫項目的代碼產生的運行庫要一致,否則將報如下錯誤

      afxver_.h(81): fatal error C1189: #error : Please use the /MD switch for _AFXDLL builds

      項目中的Runtime Library需設定的一樣

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.