jsoncpp使用示例

     下面的示例程序显示了jsoncpp的初步使用方法,包括了自定义对象的序列化等操作。
#include "json/json.h"
#include <iostream>
using namespace std;
struct Data////可序列化对象
{
	int height;
	int weight;
	void Serialize(Json::Value& root )
	{
		root["height"] = height;
		root["weight"] = weight;
	}
	void Deserialize( Json::Value& root )
	{
		height = root.get("height",0).asInt();
		weight = root.get("weight", 0).asInt();
	}
};
int main(int argc, char * argv[]) 
{
	Json::Value json_temp;      
	json_temp["name"] = Json::Value("huchao");
	json_temp["age"] = Json::Value(26);
	Json::Value root;  
	root["key_string"] = Json::Value("value_string");  
	root["key_number"] = Json::Value(12345);           
	root["key_boolean"] = Json::Value(false);          
	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("array_string2"); 	
	////对象序列化
	Data oData1; 
	oData1.height=10;
	oData1.weight=20;
	oData1.Serialize(root["oData"]);
	cout<<"Serialize ====================================="<<endl; 
	Data oData2; 
	oData2.Deserialize(root["oData"]);
	cout<<oData2.height<<","<<oData2.weight<<endl;
	/////以整数读出节点值
	cout<<"get as int =====================================";
	int num=root["key_number"].asInt();cout<<"root[\"key_number\"]="<<num<<endl;
	/////以浮点数读出
	cout<<"get as double =====================================";
	double dou=root["key_double"].asDouble();cout<<"root[\"key_double\"]="<<dou<<endl;
	////以字符串读出节点值
	cout<<"get as string =====================================";
	string str=root["key_string"].asString();cout<<"root[\"key_string\"]="<<str<<endl;
	/////判断节点是否存在
	cout<<"exsit or not =====================================";
	if(root["hahahaha"].isNull())
	{
		cout<<"no node hahahaha"<<endl;
	}
	////遍历数组节点的每个元素
	cout<<"get as array =====================================";
	int size=root["key_array"].size();
	for(int i=0;i<size;i++)
	{
		cout<<root["key_array"][i].asString()<<" ";
	}
	cout<<endl;	
	/////遍历一个json对象
	cout<<"iterator a json ====================================="<<endl;
	Json::Value::const_iterator iter1;  
	for(iter1 = root.begin(); iter1 != root.end(); iter1++)
	{
		cout<<(*iter1);
	}
	//////获得所有的keys
	cout<<"get all keys ====================================="<<endl;
	vector<string> names=root.getMemberNames();
	vector<string>::const_iterator iter2; 
	for(iter2 = names.begin(); iter2 != names.end(); iter2++)
	{
		cout<<*iter2<<" ";
	}
	cout<<endl;
	////查看json内容,使用 Writer 类即可。
	cout<<"FastWriter====================================="<<endl;
	Json::FastWriter fast_writer;
	std::cout << fast_writer.write(root);
	////Json::Reader 是用于读取的,说的确切点,是用于将字符串转换为Json::Value对象的
	Json::Reader reader;
	Json::Value json_object;
	char* json_document = "{\"age\" : 26,\"name\" : \"huchao\"}";
	if (!reader.parse(json_document, json_object))
		return 0;
	/////
	std::cout << json_object["name"];
	std::cout << json_object["age"];
}

执行结果为:

      另外其实jsoncpp的使用有两个需要注意的地方,先用代码示意:

#include "json/json.h"
#include <iostream>
using namespace std;
int main(int argc, char * argv[]) 
{
	Json::Value root; 
	/////int key = 10;root[key] = Json::Value(10.25);///////错误,json的key的类型必须是string
	Json::FastWriter fwriter;
	//////首先建立一个 Json::Value data;
	Json::Value data;
	double d1=10.45;string s1="1";data[s1]=Json::Value(d1);	
	double d2=10.5;string s2="2";data[s2]=Json::Value(d2);
	double d3=10.279;string s3="3";data[s3]=Json::Value(d3);
	/////添加
	root["mydata"] = data;
	std::cout << fwriter.write(root);
	///////修改json中的值方式一
	double d4 = root["mydata"]["1"].asDouble()+100;
	data[s1]=Json::Value(d4);/////注意data是一个局部变量,加入root时是拷贝加入不是引用,所以这里修改的还是局部变量
	std::cout << fwriter.write(root);/////并没有修改root
	root["mydata"] = data;//////修改后要提交一下
	std::cout << fwriter.write(root);
	///////修改json中的值方式二
	root["mydata"][s1] = root["mydata"][s1].asDouble()+100;
	std::cout << fwriter.write(root);
	
}
执行结果为;

 
也就是:

1.注意key只能是字符串,不能是其他类型。

2.向Json::Value中添加节点的时采用的值拷贝。

本博客的参考资料有:

1.Simple Class Serialization With JsonCpp http://www.danielsoltyka.com/programming/2011/04/15/simple-class-serialization-with-jsoncpp/

2.使用 C++ 处理 JSON 数据交换格式  http://blog.youkuaiyun.com/xt_xiaotian/article/details/5648388

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值