json

1 篇文章 0 订阅

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值