jsoncpp介绍
json是常用的协议了,jsoncpp则是cpp领域常用的json解析第三方库,可以作为生产工具!
jsoncpp 转换字符串
总共有两种方式:
1 2 3 4 5 6 7
| Json::Value root; ...
string strJson1 = root.toStyledString();
Json::FastWriter writer; string strJson1 = writer.write(root);
|
第二种方式可以去掉多余的空格和换行符,但是最后一行还是有换行符,需要自己进行去掉,例如 string.pop_up();
json简介及JsonCpp用法
json简介
json中仅支持两种结构:
- name->value键值对(pair)的集合,一般称为对象(object)。
- 值的有序表,一般称为数组(array)。
1. pair
先从键值对(pair)开始,一个pair的通常结构是:
string
:value
键值之间的对应关系使用:
表示,左边的为name,右边的为value。
一般key使用字符串,当然也可以使用数字,但是不推荐。
value的取值就比较随便,可以是任何任何json支持的类型(比如object,array,string,number,true/false,null等)。
2. object
object可以认为是多个pair的集合
其语法是以{
作为object开始,以}
作为object结束,不同的pair之间使用,
分割。
需要说明的是object中的数据存储是无序的。
下面是一个比较典型的object构成
1 2 3 4
| { "name" : "tocy", "age" : 1000 }
|
3. array
array是value的有序集合。
其语法是以[
作为array起始,以]
作为array结束,array元素之间使用,
分割。
实际使用中建议在array中使用统一的类型,否则解析起来会麻烦点。
比如下面语法是合法的:
1
| [{"name":"tocy"}, {"age":1000}, {"domain":"cn"}]
|
当然下面这种写法也是可以的,[1, "ts", true, {"key":45}]
至于其他的string和number支持的格式,建议参考json官方介绍。
JsonCpp的使用
JsonCpp,官网是:https://github.com/open-source-parsers/jsoncpp。
下载完源码之后为了使用方便,直接将源码嵌入到工程中,进入源码所在目录,先生成一个完整的头文件和cpp文件,命令如下:
然后将dist文件夹拷贝到工程目录就可以使用了。(包含json/json.h、json/json-forwards.h、json.cpp)
1.从文件中读取json文件并解析
首先我们提供一个json文件,这里命名为”checkjson.json”,其中数据如下:
1 2 3 4
| { "name" : "tocy", "age" : 1000 }
|
这里面保存的是最简单的object,我们可以使用下面代码将其读入并解析:
1 2 3 4 5 6 7 8 9 10 11 12 13
| void demo_simple() { ifstream ifs; ifs.open("checkjson.json"); assert(ifs.is_open()); Json::Reader reader; Json::Value root; if (!reader.parse(ifs, root, false)) { cerr << "parse failed \n"; return; } string name = root["name"].asString(); // 实际字段保存在这里 int age = root["age"].asInt(); // 这是整型,转化是指定类型 }
|
这里是简单的map访问,然后直接读取对应字段即可。
2.从内存中读取json数据(object)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| void demo_parse_mem_object() { const char json_data[] = "{\"name\" : \"Tocy\", \"salary\" : 100, \"msg\" : \"work hard\", \ \"files\" : [\"1.ts\", \"2.txt\"]}";
Json::Reader reader; Json::Value root; // reader将Json字符串解析到root,root将包含Json里所有子元素 if (!reader.parse(json_data, json_data + sizeof(json_data), root)) { cerr << "json parse failed\n"; return; } cout << "demo read from memory ---------\n"; string name = root["name"].asString(); int salary = root["salary"].asInt(); string msg = root["msg"].asString(); cout << "name: " << name << " salary: " << salary; cout << " msg: " << msg << endl; cout << "enter files: \n"; Json::Value files = root["files"]; // read array here for (unsigned int i = 0; i < files.size(); ++i) { cout << files[i].asString() << " "; } cout << endl << endl; }
|
3.从内存中解析json数据(array)
这次我们从提供一个以array封装的json数据,解析逻辑如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| void demo_parse_mem_array() { const char json_data[] = "[{\"name\" : \"Tocy\", \"salary\" : 100}, {\"name\" : \"Kit\", \"salary\" : 89}, \ \"a json note\"]"; Json::Reader reader; Json::Value root; // reader将Json字符串解析到root,root将包含Json里所有子元素 if (!reader.parse(json_data, json_data + sizeof(json_data), root)) { cerr << "json parse failed\n"; return; } cout << "demo read from memory using array---------\n"; unsigned int count = root.size() - 1; for (unsigned int i = 0; i < count; ++i) { string name = root[i]["name"].asString(); int salary = root[i]["salary"].asInt(); cout << "name: " << name << " salary: " << salary << endl; } cout << "last msg: " << root[count].asString() << endl; cout << endl << endl; }
|
4.简单json数据封装
1 2 3 4 5 6 7 8 9 10 11
| void demo_write_simple() { Json::Value root; Json::FastWriter writer; Json::Value person; person["name"] = "tocy"; person["age"] = 1000; root.append(person); string json_file = writer.write(root); cout << "demo write json ==============\n"; cout << json_file << endl; }
|
5. json封装-内嵌array的object
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void demo_write_object() { Json::Value root; Json::FastWriter writer; root["name"] = "tocy"; root["salary"] = 100; root["msg"] = "work hard"; Json::Value files; files[0] = "1.ts"; files[1] = "2.txt"; root["files"] = files; string json_file = writer.write(root); cout << "demo write json object ==============\n"; cout << json_file << endl; }
|
6.json封装-内嵌object的array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| void demo_write_array() { Json::Value root; Json::FastWriter writer; { Json::Value person; person["name"] = "Tocy"; person["salary"] = 100; root[0] = person; } { Json::Value person; person["name"] = "Kit"; person["salary"] = 89; root[1] = person; } root[2] = "a json note"; string json_file = writer.write(root); cout << "demo write json ==============\n"; cout << json_file << endl; }
|
构造空数组如下:{“key”:[]}
Json::Value root;
root[“key”].resize(0);
推荐
1.推荐 <大师编程之路> 公总号,里面有丰富的技术文章
2.推荐 <软件大师> 公总号,里面有丰富的破解软件文章
3.如果想看免费的电影,推荐 https://tvbibi.com 西歌视频