[C++]Json 学习笔记
Json 学习笔记JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
本文大致介绍一个较为使用的json开源库。json开源库
常用方法1.首先要引入json作用域。[code]using json = nlohmann::json;
json提供了cin,cout的输入输出流的操作符。但需要注意的是,cin要有ctr + D结束输入。cout会自动把json转换为string。
[code]#include <iostream>
#include "json.hpp"
using json = nlohmann::json;
using std::cout;
using std::endl;
using std::cin;
int main(int argc, const char * argv[]) {
json temp;
cin >> temp;
cout << temp << endl;
return 0;
}
/*
输入:
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141}
Program ended with exit code: 0
*/
2. 键值对提供根据键直接生成键值对的方法。(类似于map,如果不存在该键的话,就生成一个这个键。)
[code]#include <iostream>
#include "json.hpp"
using json = nlohmann::json;
using std::cout;
using std::endl;
using std::cin;
int main(int argc, const char * argv[]) {
// create an empty structure (null)
json j;
// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
// add a Boolean that is stored as bool
j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";
// add another null object by passing nullptr
j["nothing"] = nullptr;
// add an object inside the object
j["answer"]["everything"] = 42;
// add an array that is stored as std::vector (using an initializer list)
j["list"] = { 1, 0, 2 };
// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };
// instead, you could also write (which looks very similar to the JSON above)
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {
{"everything", 42}
}},
{"list", {1, 0, 2}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
cout << j << endl;
cout << endl;
cout << j2 << endl;
return 0;
}
/*
{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141}
{"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","nothing":null,"object":{"currency":"USD","value":42.99},"pi":3.141}
Program ended with exit code: 0
*/
3. json::array json::object:[code]#include <iostream>
#include "json.hpp"
using json = nlohmann::json;
using std::cout;
using std::endl;
using std::cin;
int main(int argc, const char * argv[]) {
// a way to express the empty array []
json empty_array_explicit = json::array();
// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = { json::array({"currency", "USD"}), json::array({"value", 42.99}) };
for (auto object : array_not_object) {
cout << object << endl;
}
return 0;
}
4. 几个区别:array是一个数组,可以用数字直接下标访问。
[code]json array = {
"yan",12,"ze",13
};
cout << array[0] << endl;
array是一个数组里面放了一个数组。认为”yan”,12是数组里面的两个元素。
[code]json array = {
{"yan",12},
"ze",13
};
cout << array[0][0] << endl;
3.array是数组里面有一个数组(里面包含了一个键值对)。
[code]json array = {
{{"yan",12}},
"ze",13
};
cout << array[0]["yan"] << endl;
4.array里面含有两个键值对。
[code]json array = {
{"yan",12},
{"ze",13}
};
cout << array["yan"] << endl;
5.array里面含有两个数组,数组里面分别有一个键值对。
[code]json array = {
{
{"yan",12}
},
{
{"ze",13}
}
};
cout << array[0]["yan"] << endl;
(注意区分{}的位置来判断对象的性质)。
如果实在判断不出来,可以用C++11的for语句,看每一个输出结果的符号来判断每一个对象的性质。
5. string转换为json可以通过增加_json来把一个string转化为相应的json对象。
[code]
int main(int argc, const char * argv[]) {
// create object from string literal
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
// or even nicer (thanks)
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
// or explicitly
auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
cout << j << endl;
cout << j2 << endl;
cout << j3 << endl;
return 0;
}
6. json转换为string得到string的表达式。
[code]// explicit conversion to string
std::string s = j.dump(); // {\"happy\":true,\"pi\":3.141}
// serialization with pretty printing
// pass in the amount of spaces to indent
std::cout << j.dump(4) << std::endl;
// {
//
"happy": true,
//
"pi": 3.141
// }
7. 与STL适应json提供了许多和STL类似的操作方法:
[code]#include <iostream>
#include "json.hpp"
using json = nlohmann::json;
using std::cout;
using std::endl;
using std::cin;
int main(int argc, const char * argv[]) {
// create an array using push_back
json j;
j.push_back("foo");
j.push_back(1);
j.push_back(true);
// iterate the array
for (json::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << *it << '\n';
}
// range-based for
for (auto element : j) {
std::cout << element << '\n';
}
// getter/setter
const std::string tmp = j[0];
j[1] = 42;
bool foo = j.at(2);
// other stuff
j.size();
// 3 entries
j.empty(); // false
j.type();
// json::value_t::array
j.clear(); // the array is empty again
// convenience type checkers
j.is_null();
j.is_boolean();
j.is_number();
j.is_object();
j.is_array();
j.is_string();
// comparison
cout << (j == "[\"foo\", 1, true]"_json) << endl; // true
// create an object
json o;
o["foo"] = 23;
o["bar"] = false;
o["baz"] = 3.141;
// special iterator member functions for objects
for (json::iterator it = o.begin(); it != o.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
}
// find an entry
if (o.find("foo") != o.end()) {
// there is an entry with key "foo"
cout << *o.find("foo") << endl;
}
// or simpler using count()
int foo_present = o.count("foo"); // 1
int fob_present = o.count("fob"); // 0
cout << foo_present << endl;
cout << fob_present << endl;
// delete an entry
o.erase("foo");
cout << (o.find("foo") == o.end()) << endl;
return 0;
}
8. STl与json转换允许把STL与json相转换。
[code]#include <iostream>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <unordered_set>
#include "json.hpp"
using json = nlohmann::json;
using std::cout;
using std::endl;
using std::cin;
int main(int argc, const char * argv[]) {
std::vector<int> c_vector {1, 2, 3, 4};
json j_vec(c_vector);
// [1, 2, 3, 4]
std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
json j_deque(c_deque);
// [1.2, 2.3, 3.4, 5.6]
std::list<bool> c_list {true, true, false, true};
json j_list(c_list);
// [true, true, false, true]
std::forward_list<int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
json j_flist(c_flist);
// [12345678909876, 23456789098765, 34567890987654, 45678909876543]
std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
json j_array(c_array);
// [1, 2, 3, 4]
std::set<std::string> c_set {"one", "two", "three", "four", "one"};
json j_set(c_set); // only one entry for "one" is used
// ["four", "one", "three", "two"]
std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
json j_uset(c_uset); // only one entry for "one" is used
// maybe ["two", "three", "four", "one"]
std::multiset<std::string> c_mset {"one", "two", "one", "four"};
json j_mset(c_mset); // only one entry for "one" is used
// maybe ["one", "two", "four"]
std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
json j_umset(c_umset); // both entries for "one" are used
// maybe ["one", "two", "one", "four"]
return 0;
}
对于关联容器,json直接把map中的键值对转化为json的object。
[code]std::map<std::string, int> c_map { {"one", 1}, {"two", 2}, {"three", 3} };
json j_map(c_map);
// {"one": 1, "three": 3, "two": 2 }
std::unordered_map<const char*, double> c_umap { {"one", 1.2}, {"two", 2.3}, {"three", 3.4} };
json j_umap(c_umap);
// {"one": 1.2, "two": 2.3, "three": 3.4}
std::multimap<std::string, bool> c_mmap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_mmap(c_mmap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
std::unordered_multimap<std::string, bool> c_ummap { {"one", true}, {"two", true}, {"three", false}, {"three", true} };
json j_ummap(c_ummap); // only one entry for key "three" is used
// maybe {"one": true, "two": true, "three": true}
json还提供隐式类型转换。
[code]/// strings
std::string s1 = "Hello, world!";
json js = s1;
std::string s2 = js;
// Booleans
bool b1 = true;
json jb = b1;
bool b2 = jb;
// numbers
int i = 42;
json jn = i;
double f = jn;
// etc.
也可以使用显式类型转换。
[code]std::string vs = js.get<std::string>();
bool vb = jb.get<bool>();
int vi = jn.get<int>();
// etc.
9. json最重要的还是设计config文件例如以下:
[code]json question = {
{"question",{
{
{"id",0},
{"choice_type","single"},
{"grading",{
{"max_grade",10},
{"half_grade",5}
}},
{"standard_answer",{1}},
{"description","this is a test for choice judger"},
{"choice",{
{
{"id",0},
{"description","nope"}
},
{
{"id",1},
{"description","B"}
},
{
{"id",2},
{"descrition","C"}
},
{
{"id",3},
{"description","D"}
}
}
}
},
{
{"id",1},
{"choice_type","double"},
{"grading",{
{"max_grade",10},
{"half_grade",5}
}},
{"standard_answer",{1, 2}},
{"description","this is a test for choice judger"},
{"choice",{
{
{"id",0},
{"description","nope"}
},
{
{"id",1},
{"description","B"}
},
{
{"id",2},
{"descrition","C"}
},
{
{"id",3},
{"description","D"}
}
}
}
},
{
{"id",2},
{"choice_type","multi"},
{"grading",{
{"max_grade",10},
{"half_grade",5}
}},
{"standard_answer",{1,2}},
{"description","this is a test for choice judger"},
{"choice",{
{
{"id",0},
{"description","nope"}
},
{
{"id",1},
{"description","B"}
},
{
{"id",2},
{"descrition","C"}
},
{
{"id",3},
{"description","D"}
}
}
}
}
}
},
{"description","this is choice questions!"}
};
json answer = {
{
{"question_id",1},
{"choice_id",{1}}
},
{
{"question_id",0},
{"choice_id",{1}}
},
{
{"question_id",2},
{"choice_id",{1,2,3}}
}
};
猜你在找