json百科:https://baike.baidu.com/item/JSON
json官网:http://www.json.org/
jsoncpp:https://github.com/open-source-parsers/jsoncpp
使用cmake编译
包含include,引入lib文件即可
例子程序:https://gitee.com/liuzhichao_rain/jsoncppExample.git
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "../include/json/json.h"
#include <string>
#include <iostream>
using namespace std;
#ifdef _DEBUG
#pragma comment(lib,"../lib/json/debug/jsoncpp.lib")
#elif NDEBUG
#pragma comment(lib,"../lib/json/release/jsoncpp.lib")
#endif
//读取json,键值对,老的方式
void ReadJsonValue()
{
string strValue = "{\"name\":\"json\"}";
Json::Value parser;
Json::Reader Reader;
if (true == Reader.parse(strValue, parser))
{
string out = parser["name"].asString();
cout << out << endl;
}
}
//封装json数据为string
//{
// "Name": "Kobe",
// "Age" : 38,
// "Language" : ["English", "Italy"],
// "E-mail" : {
// "QQ": "KobeNBA@NBA.com",
// "Hotmail" : "Kobe.Bryant@hotmail.com"
// }
//}
std::string CreateJson()
{
std::string ret;
Json::Value root, language, email;
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
root["Name"] = "Kobe";
root["Age"] = 38;
language[0] = "English";
language[1] = "Italy";
email["QQ"] = "KobeNBA@NBA.com";
email["Hotmail"] = "Kobe.Bryant@hotmail.com";
root["Language"] = language;
root["E-mail"] = email;
std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
jsonWriter->write(root, &os);
ret = os.str();
return ret;
}
//{
// "people":
// [
// {"firstName":"Kobe","lastName":"Bryant"},
// {"firstName":"Ming","lastName":"Yao"}
// ]
//}
std::string CreateJson2()
{
std::string ret;
Json::Value root,array, data1, data2;
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
data1["firstName"] = "Kobe";
data1["lastName"] = "Bryant";
data2["firstName"] = "Ming";
data2["lastName"] = "Yao";
array[0] = data1;
array[1] = data2;
root["people"] = array;
std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
jsonWriter->write(root, &os);
ret = os.str();
return ret;
}
//解析字符串
bool ParseJson(const std::string& info)
{
if (info.empty())
return false;
bool res;
JSONCPP_STRING errs;
Json::Value root, language, mail;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
res = jsonReader->parse(info.c_str(), info.c_str() + info.length(), &root, &errs);
if (!res || !errs.empty())
{
std::cout << "parseJson err." << errs << std::endl;
return false;
}
std::cout << "Name: " << root["Name"].asString() << std::endl;
std::cout << "Age: " << root["Age"].asInt() << std::endl;
language = root["Language"];
std::cout << "Language:";
for (int i = 0; i < language.size(); i++)
{
std::cout << language[i].asString();
}
std::cout << std::endl;
mail = root["E-mail"];
std::cout << "QQ:" << mail["QQ"].asString() << std::endl;
std::cout << "Hotmail:" << mail["Hotmail"].asString() << std::endl;
return true;
}
bool ParseJson2(const std::string& info)
{
if (info.empty())
return false;
bool res;
JSONCPP_STRING errs;
Json::Value root, array, data1,data2;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
res = jsonReader->parse(info.c_str(), info.c_str() + info.length(), &root, &errs);
if (!res || !errs.empty())
{
std::cout << "parseJson err." << errs << std::endl;
return false;
}
array = root["people"];
int count = array.size();
data1 = array[0];
data2 = array[1];
std::cout << "people:[" << std::endl;
std::cout << "people1 firstName:" << data1["firstName"].asString() << std::endl;
std::cout << "people1 lastName:" << data1["lastName"].asString() << std::endl;
std::cout << "people2 firstName:" << data2["firstName"].asString() << std::endl;
std::cout << "people2 lastName:" << data2["lastName"].asString() << std::endl;
return true;
}
int main()
{
ReadJsonValue();
std::cout << CreateJson() << std::endl;
std::cout << CreateJson2() << std::endl;
ParseJson(CreateJson());
ParseJson2(CreateJson2());
return 0;
}