1、下载JSON-CPP库https://sourceforge.net/projects/jsoncpp/
2、解压jsoncpp-src-0.5.0.tar
3、打开VS2010新建一个名为testJson的空控制台程序,把刚刚解压的文件中include目录和src目录的文件autolink.h、config.h、features.h、forwards.h、json.h、json_batchallocator.h、json_internalarray.inl、json_internalmap.inl、json_reader.cpp、json_value.cpp、json_valueiterator.inl、json_writer.cpp、reader.h、value.h、writer.h导入到工种。
4、新建文件一个名为main.cpp的文件,写入如下代码
#include
#include
#include "json.h"
#include
#include
using namespace std;
void readStrJson(); //从字符串中读取JSON
void readStrProJson(); //从字符串中读取JSON(内容复杂些)
int main(int argc, char *argv[])
{
//readStrJson();
char p[] = "安";
int p1 = p[0] + 256;
int p2 = p[1] + 256;
cout << "\n\n";
readStrProJson();
getchar();
return 0;
}
//从字符串中读取JSON
void readStrJson()
{
//字符串
/* const char* str =
"{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","
"\"born\":-100,\"died\":-44}"; */
char *str = "{\"state\": \"true\",\"content\": [{\"BADGE\": \"YR17100830\",\"NAME\": \"张文雄\",\"REMARK\": \"\",\"EMAIL\": \"\",\"TITLE\": \"永瑞事业一部一部生产二科\",\"JOBABBR\": \"一厂生产\",\"EMPSTATUS\": \"1\"}]}";
/*
//json内容如下:
{
"praenomen":"Gaius",
"nomen":"Julius",
"cognomen":"Caezar",
"born":-100,
"died":-44
}
*/
Json::Reader reader;
Json::Value root;
//从字符串中读取数据
if(reader.parse(str,root))
{
string praenomen = root["state"].asString();
string nomen = root["JOBABBR"].asString();
string cognomen = root["NAME"].asString();
int born = root["born"].asInt();
int died = root["died"].asInt();
cout << praenomen + " " + nomen + " " + cognomen
<< " was born in year " << born
<< ", died in year " << died << endl;
}
}
//从字符串中读取JSON(内容复杂些)
void readStrProJson()
{
//string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
char *strValue = "{\"state\": \"true\",\"content\": [{\"BADGE\": \"YR17100830\",\"NAME\": \"张文雄\",\"REMARK\": \"\",\"EMAIL\": \"\",\"TITLE\": \"永瑞事业一部一部生产二科\",\"JOBABBR\": \"一厂生产\",\"EMPSTATUS\": \"1\"}]}";
/*
//json内容如下:
{
"name": "json″,
"array": [
{
"cpp": "jsoncpp"
},
{
"java": "jsoninjava"
},
{
"php": "support"
}
]
}
*/
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value))
{
string out = value["state"].asString();
cout << out << endl;
const Json::Value arrayObj = value["content"];
for (unsigned int i = 0; i < arrayObj.size(); i++)
{
if (arrayObj[i].isMember("BADGE"))
{
out = arrayObj[i]["BADGE"].asString();
cout << "BADGE:" << out << endl;
}
if (arrayObj[i].isMember("NAME"))
{
out = arrayObj[i]["NAME"].asString();
cout << "NAME:" << out << endl;
}
if (arrayObj[i].isMember("REMARK"))
{
out = arrayObj[i]["REMARK"].asString();
cout << "REMARK:" << out << endl;
}
if (arrayObj[i].isMember("TITLE"))
{
out = arrayObj[i]["TITLE"].asString();
cout << "TITLE:" << out << endl;
}
if (arrayObj[i].isMember("JOBABBR"))
{
out = arrayObj[i]["JOBABBR"].asString();
cout << "JOBABBR:" << out << endl;
}
if (arrayObj[i].isMember("JOBABBR"))
{
out = arrayObj[i]["JOBABBR"].asString();
cout << "JOBABBR:" << out << endl;
}
if (arrayObj[i].isMember("EMPSTATUS"))
{
out = arrayObj[i]["EMPSTATUS"].asString();
cout << "EMPSTATUS:" << out << endl;
}
/*out = arrayObj[i]["cpp"].asString();
cout << out; */
/*if (i != (arrayObj.size() - 1))
cout << endl; */
}
}
}
5、解释结果如下:
6、工程源码已经上传,需要的可以下下载。