Json
Json是一种轻量级的数据交换格式(也叫数据序列化方式)。
Json采用完全独立于编程语言的文本格式 来存储和表示数据。
简洁和清晰的层次结构使得 Json 成为理想的数据交换语言。
易于人阅读和编 写,同时也易于机器解析和生成,并有效地提升网络传输效率。
JSON for Modern C++
直观的语法
整个代码由一个头文件组成 json.hpp,没有子项目,没有依赖关系,没有复杂的构建系统,使用 起来非常方便
使用 C++ 11 标准编写
使用 json 像使用 STL 容器一样
STL 和 json 容器之间可以相互转换
严谨的测试:所有类都经过严格的单元测试,覆盖了 100% 的代码,包括所有特殊的行为。此 外,还检查了 Valgrind 是否有内存泄漏。为了保持高质量,该项目遵循核心基础设施倡议(CII) 的最佳实践
json序列化
1. 将json. hpp文件加入项目中
#include "json.hpp"
using json = nlohmann: : json;
#include < iostream>
#include < vector>
#include < map>
#include< string>
using namespace std;
void func1 ( )
{
json js;
js[ "msg_type" ] = 2 ;
js[ "from" ] = "hai bao" ;
js[ "to" ] = "nan pan" ;
js[ "msg" ] = "hello , hwat are you doing now?" ;
string sendBuf = js. dump ( ) ;
cout << sendBuf. c_str ( ) << endl;
cout << js << endl;
}
void func2 ( )
{
json js1, js2;
js1[ "id" ] = { 1 , 2 , 3 , 4 , 5 } ;
js1[ "name" ] = "账单" ;
js1[ "msg" ] [ "zhang san" ] = "hello in zhang san" ;
js1[ "msg" ] [ "li si" ] = "hello im li si" ;
js2[ "msg" ] = { { "test1" , "testwords1" } , { "test2" , "testwords2" } } ;
cout << js1 << endl;
cout << js2 << endl;
}
void func3 ( )
{
json js;
vector< int> vec;
vec. push_back ( 10 ) ;
vec. push_back ( 20 ) ;
vec. push_back ( 15 ) ;
js[ "list" ] = vec;
map< int, string> m;
m. insert ( { 1 , "黄山" } ) ;
m. insert ( { 2 , "黑山" } ) ;
m. insert ( { 3 , "海豹山" } ) ;
js[ "path" ] = m;
cout << js << endl;
}
int main ( )
{
func1 ( ) ;
func2 ( ) ;
func3 ( ) ;
return 0 ;
}
json反序列化
#include "json.hpp"
using json = nlohmann: : json;
#include < iostream>
#include < vector>
#include < map>
#include< string>
using namespace std;
string func1 ( )
{
json js;
js[ "msg_type" ] = 2 ;
js[ "from" ] = "hai bao" ;
js[ "to" ] = "nan pan" ;
js[ "msg" ] = "hello , hwat are you doing now?" ;
string sendBuf = js. dump ( ) ;
cout << sendBuf. c_str ( ) << endl;
return sendBuf;
}
string func2 ( )
{
json js1, js2;
js1[ "id" ] = { 1 , 2 , 3 , 4 , 5 } ;
js1[ "name" ] = "账单" ;
js1[ "msg" ] [ "zhang san" ] = "hello in zhang san" ;
js1[ "msg" ] [ "li si" ] = "hello im li si" ;
js2[ "msg" ] = { { "test1" , "testwords1" } , { "test2" , "testwords2" } } ;
return js1. dump ( ) ;
}
string func3 ( )
{
json js;
vector< int> vec;
vec. push_back ( 10 ) ;
vec. push_back ( 20 ) ;
vec. push_back ( 15 ) ;
js[ "list" ] = vec;
map< int, string> m;
m. insert ( { 1 , "黄山" } ) ;
m. insert ( { 2 , "黑山" } ) ;
m. insert ( { 3 , "海豹山" } ) ;
js[ "path" ] = m;
return js. dump ( ) ;
}
int main ( )
{
string recvBuf = func1 ( ) ;
json jsBuf = json: : parse ( recvBuf) ;
cout << "msg_type = " << jsBuf[ "msg_type" ] << endl;
string str2 = func2 ( ) ;
json jsbuf2 = json: : parse ( str2) ;
cout << jsbuf2[ "id" ] << endl;
cout << jsbuf2[ "msg" ] << endl;
cout << jsbuf2[ "msg" ] [ "li si" ] << endl;
auto it = jsbuf2[ "msg" ] ;
cout << it[ "li si" ] << endl;
string str3 = func3 ( ) ;
json jsbuf3 = json: : parse ( str3) ;
vector< int> vec = jsbuf3[ "list" ] ;
map< int, string> m = jsbuf3[ "path" ] ;
for ( auto & a : vec)
{
cout << a << endl;
}
for ( auto & a : m)
{
cout << a. second << endl;
}
return 0 ;
}