一、自行編譯json庫
1、先在github上下載源碼, json源碼下載地址
2、打開makefiles\msvc2010\jsoncpp.sln(利用vs2013打開),會出現三個Project:jsontest, lib_json, test_lib_json
3、選擇中間的lib_json項目,右鍵屬性 —》C/C++ —》代碼生成—》運行庫,選擇多線程DLL(/MDd)
4、還是lib_json項目,右鍵重新生成,就會生成一個lib_json.lib文件。
5、將include\json文件夾下的.h文件拷貝出來,加入到自己的testjson工程包含目錄下,將lib_json.lib拷貝到testjson工程的庫目錄下,並在配置屬性—》鏈接器—》輸入中,添加lib_json.lib
6、開始調試自己的testjson工程,可以使用json了。
二、直接下載編譯好的庫
下載地址爲:https://download.csdn.net/download/lly_117/10430803
使用的例子:
#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;
}
其中運行結果如圖所示:
參考博客:
https://blog.csdn.net/hust_bochu_xuchao/article/details/78516864
https://www.cnblogs.com/cuish/p/3888657.html