Jsoncpp 使用方法大全
Json(JavaScript Object Notation )是一种轻量级的数据交换格式。简而言之,Json组织形式就和python中的字典, C/C++中的map一样,是通过key-value对来组织的,key是任意一个唯一字符串,value可以是bool,int,string 或者嵌套的一个json。关于Json 格式可以参考官方网站。 Jsoncpp 常用变量介绍在Jsoncpp中,有几个常用的变量特别重要,首先介绍一下。 Json::ValueJson::Value 用来表示Json中的任何一种value抽象数据类型,具体来说,Json中的value可以是一下数据类型:
可以通过
Json::ReaderJson::Reader可以通过对Json源目标进行解析,得到一个解析好了的Json::Value,通常字符串或者文件输入流可以作为源目标。 假设现在有一个example.json文件 5 使用Json::Reader对Json文件进行解析:2
bool parse (const std::string &document,Value &root,bool collectComments=true)
bool parse (std::istream &is,136)">true)
9 使用Json::Reader对字符串进行解析5
bool Json::Reader::parse ( const char * beginDoc,136)">char * endDoc,Value & root,136)">bool collectComments = true
)
9
Json::Value root;
Json::Reader reader;
const char* s = "{"uploadid": "UP000000","code": 100,"msg": "files"}";
if(!reader.parse(s,root)){
// "parse fail";
}
else{
std::cout << root["uploadid"].asString();//print "UP000000"
}
Json::Writer Json::Writer 和 Json::Reader相反,是把Json::Value对象写到string对象中,而且Json::Writer是个抽象类,被两个子类Json::FastWriter和Json::StyledWriter继承。 13 结果: true } } example_fast_writer.json 1
{"encoding" : plug-ins" : ["ruby"],102)">true}}
Jsoncpp 其他操作通过前面介绍的Json::value,Json::Reader,Json::Reader 可以实现对Json文件的基本操作,下面介绍一些其他的常用的操作。 判断key是否存在10
bool Json::Value::isMember ( char * key) const
Return true if the object has a member named key.
Note
'key' must be null-terminated.
bool Json::Value::isMember ( const std::string & const
bool Json::Value::isMember ( char* key,136)">end ) const
15
// print "encoding is a member"
if(root.isMember("encoding")){
cout<<"encoding is a member"<<std::endl;
}
else{
"encoding is not a member"<<std::endl;
}
// print "encode is not a member"
"encode")){
"encode is a member"<<"encode is not a member"<<std::endl;
}
判断Value是否为null首先要给example.json添加一个key-value对: 11
{
"encoding" : true },"tab-length":[],"tab":null }
判断是否为null的成员函数 1
bool Json::Value::isNull ( ) 3
if(root["tab"].isNull()){
cout << "isNull" <<std::endl;//print isNull
}
9
"tab-length")){//true
"tab-length"].isNull()){
"isNull" << std::endl;
}
else "not Null"<<std::endl;
// print "not Null",there is a array object([]),through this array object is empty
"empty: " << root["tab-length"].empty() << //print empty: 1
"size: " << root["tab-length"].size() << //print size: 0
}
另外值得强调的是,Json::Value和C++中的map有一个共同的特点,就是当你尝试访问一个不存在的 key 时,会自动生成这样一个key-value默认为null的值对。也就是说 2
root["anything-not-exist"].isNull(); //false
root.isMember("anything-not-exist"); //true
总结就是要判断是否含有key,使用isMember成员函数,value是否为null使用isNull成员函数,value是否为空可以用empty() 和 size()成员函数。 得到所有的key15
typedef std::vector<string> Json::Value::Members
Value::Members Json::Value::getMemberNames ( ) const
Return a list of the member names.
If null,136)">return an empty list.
Precondition
type() is objectValue or nullValue
Postcondition
if type() was nullValue,it remains nullValue
可以看到Json::Value::Members实际上就是一个值为string的vector,通过getMemberNames得到所有的key。 删除成员19 参考http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |