jsoncpp的入门学习

前端之家收集整理的这篇文章主要介绍了jsoncpp的入门学习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

JsonCpp是一个开源的跨平台库,实现C++与Json数据的解析与相互转换,轻量级,易使用。项目地址:http://jsoncpp.sourceforge.net/

这里有个比较不错的介绍:

http://www.cnblogs.com/kex1n/archive/2011/12/02/2272328.html


首先下载源码,编译,默认为vs71工程,我用的vs2010进行编译,可以直接编译通过。但是Release版本库使用时提示缺少 xxx.asm文件解决办法:

在Release下

1) 打开lib_json -> Properties -> Configuration Properties -> C/C++ -> Output Files -> Assembler Output
2) 更改为No Listing

如此编译后就不再依赖xxx.asm文件了。


下面贴两段代码

//JSON读取
	std::string strValue="{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}"; 
	Json::Reader reader;
	Json::Value value;
	
	if(reader.parse(strValue,value))
	{
		std::string out=value["key1"].asString();
		std::cout<<out<<std::endl;
		const Json::Value arrayObj=value["array"];
        for (int i=0; i < arrayObj.size();i++)  
		{
			out=arrayObj[i]["key2"].asString();
			std::cout<<out;
			if(i!=arrayObj.size()-1)
				std::cout<<std::endl;;
		}
	}
//JSON写入
	Json::Value root;
	Json::FastWriter writer;
	Json::Value user;
	Json::Value data;

	user["userAccount"] = "admin";
	user["password"] = "123456";
	user["username"] = "张三";
	user["idcardno"] = "460033199004062314";
	user["syscode"] = "01";
	user["sysfunid"] = "0101";

	data.append(user);
	root["data"]  = data;
	std::string json_file = writer.write(root);

	std::ofstream ofs;
	ofs.open("test1.json");
	assert(ofs.is_open());
	ofs<<json_file;
原文链接:https://www.f2er.com/json/290220.html

猜你在找的Json相关文章