add:test json 1215
This commit is contained in:
parent
5ac88c0cf8
commit
81625bcc6b
25044
testjson/json.hpp
Normal file
25044
testjson/json.hpp
Normal file
File diff suppressed because it is too large
Load Diff
BIN
testjson/testjson
Executable file
BIN
testjson/testjson
Executable file
Binary file not shown.
91
testjson/testjson.cpp
Normal file
91
testjson/testjson.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
#include "json.hpp"
|
||||
using json = nlohmann::json;
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
string fun1(){
|
||||
json js;
|
||||
js["msg_type"] = 2;
|
||||
js["from"] = "zhangsan";
|
||||
js["to"] = "lisi";
|
||||
js["msg"] = "hello";
|
||||
|
||||
cout << js << endl;
|
||||
string sendjs = js.dump();
|
||||
cout << sendjs.c_str() << endl;
|
||||
|
||||
return sendjs;
|
||||
}
|
||||
|
||||
string fun2(){
|
||||
json js2;
|
||||
js2["msg_id"] = {1,2,3,4,5};
|
||||
js2["msg"]["msg_a"] = "a";
|
||||
js2["msg"]["msg_b"] = "b";
|
||||
js2["msg"] = {{"msg_aa","aa"},{"msg_bb","bb"}};
|
||||
|
||||
cout << js2 <<endl;
|
||||
string sendjs2 = js2.dump();
|
||||
cout << sendjs2.c_str() << endl;
|
||||
return sendjs2;
|
||||
}
|
||||
|
||||
string fun3(){
|
||||
vector<int> v;
|
||||
v.push_back(1);
|
||||
v.push_back(2);
|
||||
v.push_back(3);
|
||||
|
||||
json js3;
|
||||
js3["j_vec"] = v;
|
||||
|
||||
map<int,string> m;
|
||||
m[1] = "m1";
|
||||
m[2] = "m2";
|
||||
m.insert({3,"m3"});
|
||||
|
||||
js3["j_map"] = m;
|
||||
|
||||
cout << js3 << endl;
|
||||
string sendjs3 = js3.dump();
|
||||
cout << sendjs3.c_str() << endl;
|
||||
|
||||
return sendjs3;
|
||||
}
|
||||
|
||||
int main(){
|
||||
//fun1();
|
||||
//fun2();
|
||||
//fun3();
|
||||
|
||||
string recvjs = fun1();
|
||||
json js = json::parse(recvjs);
|
||||
cout << js <<endl;
|
||||
|
||||
string recvjs2 = fun2();
|
||||
json js2 = json::parse(recvjs2);
|
||||
cout << js2["msg_id"] <<endl;
|
||||
auto arr = js2["msg_id"];
|
||||
cout << arr[2] << endl;
|
||||
|
||||
string arcvjs3 = fun3();
|
||||
json js3 = json::parse(arcvjs3);
|
||||
vector<int> v = js3["j_vec"];
|
||||
map<int,string> m = js3["j_map"];
|
||||
for(int i : v){
|
||||
cout << i;
|
||||
}
|
||||
cout << endl;
|
||||
for(auto j : m){
|
||||
cout << j.first <<" " << j.second << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user