Json解析数组实例
2014-11-10 21:14
302 查看
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<string>而已。
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
#pragma once
#pragma comment(lib, "json_vc71_libmtd.lib")
#include "json/json.h"
实现
[cpp] view
plaincopy
void CJSONTestDlg::OnBnClickedButton1()
{
CEdit* pEdit =(CEdit*)GetDlgItem(IDC_EDIT1);
CString str;
pEdit->GetWindowText(str); //str即为a)中定义的JSON串
pEdit->FmtLines(true);
Json::Reader reader;
Json::Value root;
if (reader.parse(WC2UT(str.GetBuffer(0)), root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
std::string JsonID = root["JsonID"].asString();
Json::Value rtnRoot;
rtnRoot["JsonID"]=JsonID;
Json::Value ack;
Json::Value send = root["Send"];
if(!send["Items"].isNull()){
Json::Value Items = send["Items"];
int sendSize = Items.size();
for(int i=0;i<sendSize;i++){//循环获取到JSON串数组当中的值
std::string Code = Items[i]["Code"].asString();
int x = Items[i]["X"].asInt();
int y = Items[i]["Y"].asInt();
int count = Items[i]["Count"].asInt();
//更具读到的JSON串中的值生成所需内容
Json::Value newAckItem;
newAckItem["Code"]=Code;
newAckItem["ActualCount"]=count;
ack.append(newAckItem);
}
}
rtnRoot["Ack"]=ack;
std::string rtnOut = rtnRoot.toStyledString();//生成带格式的JSON串
#ifdef UNICODE
std::wstring stemp = s2ws(rtnOut);
LPCWSTR result = stemp.c_str();
#else
LPCWSTR result = rtnOut.c_str();
#endif
MessageBox(result,_T("根据JSON串,生成的对应JSON串信息"));
CEdit* pEdit =(CEdit*)GetDlgItem(IDC_EDIT2);
pEdit->SetWindowText(result);
}else{
CEdit* pRtnEdit =(CEdit*)GetDlgItem(IDC_EDIT2);
pRtnEdit->SetWindowText(_T("JSON格式错误"));
}
}
将JSONCPP以静态库方式导入,需要注意项目中的代码生成中的运行库,和JSONCPP的静态库项目的代码生成的运行库要一致,否则将报如下错误
afxver_.h(81): fatal error C1189: #error : Please use the /MD switch for _AFXDLL builds
项目中的Runtime Library需设置的一样
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<string>而已。
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
#pragma once
#pragma comment(lib, "json_vc71_libmtd.lib")
#include "json/json.h"
实现
[cpp] view
plaincopy
void CJSONTestDlg::OnBnClickedButton1()
{
CEdit* pEdit =(CEdit*)GetDlgItem(IDC_EDIT1);
CString str;
pEdit->GetWindowText(str); //str即为a)中定义的JSON串
pEdit->FmtLines(true);
Json::Reader reader;
Json::Value root;
if (reader.parse(WC2UT(str.GetBuffer(0)), root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
std::string JsonID = root["JsonID"].asString();
Json::Value rtnRoot;
rtnRoot["JsonID"]=JsonID;
Json::Value ack;
Json::Value send = root["Send"];
if(!send["Items"].isNull()){
Json::Value Items = send["Items"];
int sendSize = Items.size();
for(int i=0;i<sendSize;i++){//循环获取到JSON串数组当中的值
std::string Code = Items[i]["Code"].asString();
int x = Items[i]["X"].asInt();
int y = Items[i]["Y"].asInt();
int count = Items[i]["Count"].asInt();
//更具读到的JSON串中的值生成所需内容
Json::Value newAckItem;
newAckItem["Code"]=Code;
newAckItem["ActualCount"]=count;
ack.append(newAckItem);
}
}
rtnRoot["Ack"]=ack;
std::string rtnOut = rtnRoot.toStyledString();//生成带格式的JSON串
#ifdef UNICODE
std::wstring stemp = s2ws(rtnOut);
LPCWSTR result = stemp.c_str();
#else
LPCWSTR result = rtnOut.c_str();
#endif
MessageBox(result,_T("根据JSON串,生成的对应JSON串信息"));
CEdit* pEdit =(CEdit*)GetDlgItem(IDC_EDIT2);
pEdit->SetWindowText(result);
}else{
CEdit* pRtnEdit =(CEdit*)GetDlgItem(IDC_EDIT2);
pRtnEdit->SetWindowText(_T("JSON格式错误"));
}
}
将JSONCPP以静态库方式导入,需要注意项目中的代码生成中的运行库,和JSONCPP的静态库项目的代码生成的运行库要一致,否则将报如下错误
afxver_.h(81): fatal error C1189: #error : Please use the /MD switch for _AFXDLL builds
项目中的Runtime Library需设置的一样
相关文章推荐
- Android—JSON数据解析(带数组)实例
- 实例详解PHP serialize与JSON解析 --转
- JSON 之 SuperObject(16): 实例 - 解析 Google 关键字搜索排名
- JSON数组解析代码样例
- Android如何解析json数组对象
- android端使用json解析调用Google Directions API返回结果的实例
- 实例解析C++/CLI之头文件、内联函数与数组
- JS解析json数据(如何将json字符串转化为数组)
- Flex解析Json数组
- JS解析json数据(如何将json字符串转化为数组)
- json数组解析
- jquery下json数组的操作用法实例
- Google Gson解析Json数据应用实例
- Ajax使用方法,经典Ajax封装,可接收解析后台传回的json对象和json数组
- [转载]jquery遍历筛选数组的几种方法和遍历解析json对象
- 实例详解PHP serialize与JSON解析
- 一个JSON 实例: jQuery 解析JSON数据
- 实例解析java + jQuery + json工作过程(登录)
- [Android实例] Android JSON解析示例代码
- android,PHP解析JSON数组,JSONArray,JSONOject,乱码