RapidJSON 是一个 C++ 的 JSON 解析器及生成器,具体相关信息可参考 RapidJSON 文档 ,在 Cocos2d-x 3.0 中加入了 rapidjson 库用于 json 解析,该库位于 external/json 下。
下面就来说说如何使用 rapidjson 来解析和生成 json 字符串:
1. 解析 json 字符串
使用 rapidjson 来解析下面的 json 字符串内容
{
"success": true,
"code": 0,
"result": {
"order": {
"title": "标题",
"detail": "订单详情",
"extends": "扩展内容",
"time_start": "1518338445",
"time_expire": "1518438445"
},
"order_id": "20180211-108170"
}
}
引入头文件
#include "json/rapidjson.h"
#include "json/document.h"
json 解析
std::string str_json = "{\"success\": true, \"code\": 0, \"result\": {\"order\": {\"title\": \"标题\", \"detail\": \"订单详情\", \"extends\": \"扩展内容\", \"time_start\": \"1518338445\", \"time_expire\": \"1518438445\"}, \"order_id\": \"20180211-108170\"}}";
CCLOG("%s\n", str_json.c_str());
rapidjson::Document document;
document.Parse<0>(str_json.c_str());
if (document.HasParseError()) { //打印解析错误
CCLOG("GetParseError %s\n",document.GetParseError());
}
if (document.IsObject()) {
if (document.HasMember("success")) {
CCLOG("%s\n", document["success"].GetBool()); //打印获取 success 的值
}
if (document.HasMember("result") && document["result"].HasMember("order_id") ) {
CCLOG("%s\n", document["result"]["order_id"].GetString()); //打印获取 order_id 的值
}
}
2. 生成 json 字符串
引入头文件
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
using namespace rapidjson;
生成 json 内容
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value array(rapidjson::kArrayType);
rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("int", 1, allocator);
object.AddMember("double", 1.0, allocator); object.AddMember("bool", true, allocator); object.AddMember("hello", "你好", allocator); array.PushBack(object, allocator);
document.AddMember("json", "json string", allocator); document.AddMember("array", array, allocator);
StringBuffer buffer;
rapidjson::Writer<StringBuffer> writer(buffer);
document.Accept(writer);
CCLOG("%s",buffer.GetString());
打印结果
cocos2d: {"json":"json string","array":[{"int":1,"double":1,"bool":true,"hello":"你好"}]}
3. json解析+生成结合使用(适合快速提取 json 字符串中的某段内容)
比如从刚刚的内容中,获取 order 字段对应的 json 内容
原json内容:
{
"success": true,
"code": 0,
"result": {
"order": {
"title": "标题",
"detail": "订单详情",
"extends": "扩展内容",
"time_start": "1518338445",
"time_expire": "1518438445"
},
"order_id": "20180211-108170"
}
}
需要提取的json内容:
{
"title": "标题",
"detail": "订单详情",
"extends": "扩展内容",
"time_start": "1518338445",
"time_expire": "1518438445"
}
思路:解析原有json字符串,获取到 order 字段对应的 object 后,再将内容生成到新的 json 字符串中:
std::string responseStr = "{\"success\": true, \"code\": 0, \"result\": {\"order\": {\"title\": \"标题\", \"detail\": \"订单详情\", \"extends\": \"扩展内容\", \"time_start\": \"1518338445\", \"time_expire\": \"1518438445\"}, \"order_id\": \"20180211-108170\"}}"
rapidjson::Document d;
d.Parse<rapidjson::kParseDefaultFlags>(responseStr.c_str());
if (d.HasParseError()) { //打印解析错误
log("GetParseError %s", d.GetParseError());
}
if (d.IsObject() && d.HasMember("result")) {
if (d["result"].HasMember("order")) {
rapidjson::Document doc;
doc.Set(d["result"]["order"].GetObject(), doc.GetAllocator());
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
doc.Accept(writer);
std::string order_info=sb.GetString();
log("GetParseJson order_info=%s",order_info.c_str());
}
}
网友评论