1.下载源码
从Github下载源码(C++),链接:https://github.com/Eureca2017/jsoncpp,如图1所示。
2.解压编译D:\StudyRoad\Install\jsoncpp-master\makefiles\vs71下的工程,可仅编译生成lib_json工程,均使用unicode字符集。
Debug下:运行库MDd,输出文件:无列表
Release下:运行库MD,输出文件:无列表
最后生成的lib文件分别位于:jsoncpp-master\build\vs71\debug\lib_json、jsoncpp-master\build\vs71\release\lib_json
3.配置包含目录、库目录以及附加依赖项。注意运行库应当根据实际需要设置。
运行库:debug:MDd
release:MD
包含目录:....../jsoncpp-master\include
库目录:debug下:....../jsoncpp-master\build\vs71\debug\lib_json
release下:......./jsoncpp-master\build\vs71\release\lib_json
附加依赖项:debug下:json_vc71_libmtd.lib
release下:json_vc71_libmt.lib
4.测试
// jsontest2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include<json/json.h>
using namespace std;
int main()
{
char* strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }";
Json::CharReaderBuilder b;
Json::CharReader* reader(b.newCharReader());
JSONCPP_STRING errs;
Json::Value value;
bool bRet = reader->parse(strStudent, strStudent + std::strlen(strStudent), &value, &errs);
if (false == bRet)
{
cerr << endl << "read value error \n";
return -1;
}
cout << value["name"].asString() << endl;
cout << value["age"].asInt() << endl;
cout << value["major"].asString() << endl;
cout << endl;
Json::Value json_temp;
json_temp["name"] = Json::Value("cuihao");
json_temp["age"] = Json::Value(28);
Json::Value root;
root["key_string"] = Json::Value("value_string");
root["key_number"] = Json::Value(12345);
root["key_boolean"] = Json::Value(true);
root["key_double"] = Json::Value(12.345);
root["key_object"] = json_temp;
root["key_array"].append("array_string1");
root["key_array"].append("array_string2");
root["key_array"].append(12345);
Json::ValueType type = root.type();
Json::Value arrValue = root["key_array"];
for (Json::Value::UInt i = 0; i < arrValue.size(); ++i)
{
if (arrValue[i].isString())
cout << arrValue[i].asString();
else if (arrValue[i].isInt())
cout << arrValue[i].asInt();
cout << endl;
}
cout << endl << "----------------------------------- " << endl;
string strTemp =
"{ \"name\" : \"cuihao\" ," \
" \"age\" : 28 }";
string strRoot =
"{ \"key_string\" : \"value_string\", " \
" \"key_number\" : 28, " \
" \"key_boolean\" : false, " \
" \"key_double\" : 12.345, " \
" \"key_object\" : json_temp, " \
" \"key_array\" : [\"array_string1\", \"array_string2\", 12345]}";
Json::StreamWriterBuilder wbuilder;
wbuilder["indentation"] = "";
cout << endl << Json::writeString(wbuilder, root) << endl;
cout << endl << "----------------------------" << endl;
Json::Value::Members members = root.getMemberNames();
for (Json::Value::Members::const_iterator iter = members.begin();
iter != members.end();
++iter)
{
string strName = *iter;
if (root[strName].isInt())
cout << root[strName].asInt();
else if (root[strName].isString())
cout << root[strName].asString();
else if (root[strName].isDouble())
cout << root[strName].asDouble();
else if (root[strName].isBool())
cout << root[strName].asBool();
else if (root[strName].isArray())
{
for (Json::Value::ArrayIndex i = 0; i < root[strName].size(); ++i)
{
if (root[strName][i].isInt())
cout << root[strName][i].asInt();
else if (root[strName][i].isString())
cout << root[strName][i].asString();
else
cout << "others";
cout << endl;
}
}
else if (root[strName].isObject())
{
Json::Value::Members mbs = root[strName].getMemberNames();
for (Json::Value::Members::const_iterator iter2 = mbs.begin();
iter2 != mbs.end();
++iter2)
{
string strName2 = *iter2;
if (root[strName][strName2].isInt())
cout << root[strName][strName2].asInt();
else if (root[strName][strName2].isString())
cout << root[strName][strName2].asString();
else
cout << "others";
cout << endl;
} //for
} //else if
else
cout << "others";
cout << endl;
}
system("pause");
return 0;
}
成功输入如下: