javascript代码实例教程-Json解析数组实例

发布时间:2019-01-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-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需设置的一样

      javascript代码实例教程-Json解析数组实例

      觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

      脚本宝典总结

      以上是脚本宝典为你收集整理的javascript代码实例教程-Json解析数组实例全部内容,希望文章能够帮你解决javascript代码实例教程-Json解析数组实例所遇到的问题。

      如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

      本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
      如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。