这次我们把JSON格式在复杂一点:
比如这样的格式:
{"":[
{"":"","":""},
{"":"","":""},
{"":"","":""}]}
具体数据:
{"people":[
{"Name":"Eagle","Age":"18","Email":"eaglezzb@gmail.com"},
{"Name":"Porter","Age":"18","Email":"pDDDr@gmail.com"},
{"Name":"EVAL","Age":"18","Email":"EDDa@gmail.com"}]}
下面是完整的编译代码:
有这几个,基本上什么数据都可以用起来了
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include "json.h"
TCHAR szFullConfName[2*MAX_PATH + 1] = {0};
void saveStringToFile(void)
{
TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0;//删除文件名,只获得路径
TCHAR str_url[2*MAX_PATH + 1] = {0};
lstrcat(str_url, szFilePath);
lstrcat(str_url, _T("msg.json") );
//only for log
lstrcat(szFullConfName, str_url);
//
}
int main(int argc, char* argv[])
{
//解析json
//--======================Reader file====================================--
// Reader file and show Json
saveStringToFile();
Json::Value jsReadValue;
std::ifstream in(szFullConfName);
std::string szReadstr;
std::string szReadmsg;
if(in.is_open())
{
while(in>>szReadstr)
szReadmsg += szReadstr;
}
Json::Reader jsReader;
int ret = jsReader.parse(szReadmsg,jsReadValue);
if(ret)
{
/**
{"":[
{"":"","":""},
{"":"","":""},
{"":"","":""}]}
**/
Json::Value jsArrayReaderValue ;
jsArrayReaderValue = jsReadValue["people"];
int iASize = jsArrayReaderValue.size();
for ( int i=0; i < iASize;i++ )
{
std::string szName =jsArrayReaderValue[i]["Name"].asString();
std::string szAge =jsArrayReaderValue[i]["Age"].asString();
std::string szEmail =jsArrayReaderValue[i]["Email"].asString();
std::cout<<"Name:"<<szName<<" Age: "<<szAge<<"E-Mail: "<<szEmail<< std::endl;
}
}
in.close();
return 0;
}