rapidjson的使用
来源:互联网 发布:亲宝宝软件下载 编辑:程序博客网 时间:2024/05/18 05:58
rapidjson用来解析和生成json数据
1、解析json数据
json数据如下图
{ "FlgStatus": "sample string 1", "OrderNo": true, "CustomerContactID": 123.45, "CouponType": 1233435454, "ordersBody": [ { "CustomerContactID": "sample string 1", "CustomerID": false, "Provinces": 123.45, "City": 34545 }, { "CustomerContactID": "sample string 1", "CustomerID": false, "Provinces": 123.45, "City": 34545 } ], "customercontact": { "CustomerContactID": "sample string 1", "CustomerID": false, "Provinces": 123.45, "City": 34545 }}
解析json数据,详情见注释
#include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/filestream.h" #include "rapidjson/stringbuffer.h" #define psln(x) std::cout << #x " = " << (x) << std::endlusing std::string; using std::ifstream; string stringFromStream; ifstream in; in.open("E://json1.json", ifstream::in); if (!in.is_open()) return; string line; while (getline(in, line)) { stringFromStream.append(line + "\n"); } in.close(); //cout << stringFromStream << endl; Document doc; doc.Parse<0>(stringFromStream.c_str()); if (doc.HasParseError()) { //ParseErrorCode code = doc.GetParseError(); psln("error"); return; } // use values in parse result. Value & v = doc["FlgStatus"]; if (v.IsString()) { psln(v.GetString()); } v = doc["OrderNo"]; if (v.IsBool()) { psln(v.GetBool()); } v = doc["CustomerContactID"]; if (v.IsDouble()) { psln(v.GetDouble()); } v = doc["CouponType"]; if (v.IsInt()) { psln(v.GetInt()); } //解析array v = doc["ordersBody"]; if (v.IsArray()) { int len = v.Size(); for (int i = 0; i < len; i++) { Value & value = v[i]; if (value.IsObject()) { if (value.HasMember("CustomerContactID") && value["CustomerContactID"].IsString()) { psln(value["CustomerContactID"].GetString()); } if (value.HasMember("CustomerID") && value["CustomerID"].IsBool()) { psln(value["CustomerID"].GetBool()); } if (value.HasMember("Provinces") && value["Provinces"].IsDouble()) { psln(value["Provinces"].GetDouble()); } if (value.HasMember("City") && value["City"].IsInt()) { psln(value["City"].GetInt()); } } } } //解析object v = doc["customercontact"]; if (v.IsObject()) { if (v.HasMember("CustomerContactID") && v["CustomerContactID"].IsString()) { psln(v["CustomerContactID"].GetString()); } if (v.HasMember("CustomerID") && v["CustomerID"].IsBool()) { psln(v["CustomerID"].GetBool()); } if (v.HasMember("Provinces") && v["Provinces"].IsDouble()) { psln(v["Provinces"].GetDouble()); } if (v.HasMember("City") && v["City"].IsInt()) { psln(v["City"].GetInt()); } }
2、生成json数据,格式和上面一样
Document document; Document::AllocatorType& allocatorType = document.GetAllocator(); Value root(kObjectType); //添加string数据 Value FlgStatus="sample string 1"; root.AddMember("FlgStatus", FlgStatus, allocatorType); //添加bool Value OrderNo=true; root.AddMember("OrderNo", OrderNo, allocatorType); //添加int Value CustomerContactID=123; root.AddMember("CustomerContactID", CustomerContactID, allocatorType); //添加double Value CouponType = 123.456; root.AddMember("CouponType", CouponType, allocatorType); //添加array Value ordersBody(kArrayType); for (int i = 0; i < 2; i++) { Value v(kObjectType); Value CustomerContactID = "sample string 1"; v.AddMember("CustomerContactID", CustomerContactID, allocatorType); Value CustomerID = false; v.AddMember("CustomerID", CustomerID, allocatorType); Value Provinces = 123.45; v.AddMember("Provinces", Provinces, allocatorType); Value City = 34545; v.AddMember("City", City, allocatorType); ordersBody.PushBack(v,allocatorType); } root.AddMember("ordersBody", ordersBody, allocatorType); //添加object数据 Value customercontact(kObjectType); Value CustomerContactID1 = "sample string 1"; customercontact.AddMember("CustomerContactID", CustomerContactID1, allocatorType); Value CustomerID = false; customercontact.AddMember("CustomerID", CustomerID, allocatorType); Value Provinces = 123.45; customercontact.AddMember("Provinces", Provinces, allocatorType); Value City = 34545; customercontact.AddMember("City", City, allocatorType); root.AddMember("customercontact", customercontact, allocatorType); StringBuffer stringBuffer; Writer<StringBuffer> writer(stringBuffer); root.Accept(writer); std::string result = stringBuffer.GetString(); cout << "result: " << result.c_str() << "..........:" << result.size() << endl; ofstream outfile("E://json2.json"); outfile << result.c_str() << endl; outfile.close();
更多详细教程
项目下载地址
0 0
- rapidjson库的使用
- RapidJSON的使用
- rapidJson 的使用
- rapidjson的使用
- rapidjson的使用例子
- rapidjson的使用
- rapidjson库的基本使用
- Rapidjson的简单使用示例
- rapidjson使用
- rapidjson 使用
- rapidjson 使用
- rapidjson常见使用示例
- rapidjson使用总结
- rapidjson 使用例子
- rapidjson使用样例
- rapidjson 使用总结
- rapidjson简单使用
- rapidjson使用问题小计
- Windows下MySQL-zip方式安装
- UICollectionView的常用属性和方法
- Atomic write, 揭开你的面纱
- Android之This element neither has attached source nor attached Javadoc and hence no Javadoc could be
- log4j 操作
- rapidjson的使用
- android studio常用快捷键
- 整理---从手机相册获取图片的方法
- imbue
- 剑指offer之面试题23:从上往下打印二叉树
- vijos p1233
- Spring—— Spring Core
- zoj3946 Highway Project(单源最短路径)
- Xmanager4.0注册码