利用rapidjson实现解析嵌套的json的方法示例
2019/7/10 22:27:39
本文主要是介绍利用rapidjson实现解析嵌套的json的方法示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
利用rapidjson解析嵌套的json
看json串1:{"system":{"version":"v2.6.1", "name":"value"}}
废话少说, 直接撸代码:
#include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<sstream> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" #include "rapidjson/memorystream.h" using namespace std; using rapidjson::Document; using rapidjson::StringBuffer; using rapidjson::Writer; using namespace rapidjson; string getVersion(const string &jvStr) { Document document; if (document.Parse(jvStr.c_str()).HasParseError() || !document.HasMember("system")) { return ""; } const rapidjson::Value &jvObject = document["system"]; if(!jvObject.IsObject()) { return ""; } if(!jvObject.HasMember("version")) { return ""; } const rapidjson::Value &jv = jvObject["version"]; return jv.GetString(); } int main(int argc, char *argv[]) { string s = "{\"system\":{\"version\":\"v2.6.1\", \"name\":\"value\"}}"; cout << s << endl; cout << getVersion(s) << endl; return 0; }
结果:
{"system":{"version":"v2.6.1", "name":"value"}}
v2.6.1
再看字符串:{"system": "{\"version\":\"v2.6.1\", \"name\":\"value\"}"}
直接上马:
#include <iostream> #include <stdio.h> #include<unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<sstream> // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/rapidjson.h" #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" #include "rapidjson/memorystream.h" using namespace std; using rapidjson::Document; using rapidjson::StringBuffer; using rapidjson::Writer; using namespace rapidjson; string getStringFromJson(const string &jsStr, const string &strKey) { Document document; if (document.Parse(jsStr.c_str()).HasParseError() || !document.HasMember(strKey.c_str())) { return ""; } const rapidjson::Value &jv = document[strKey.c_str()]; return jv.GetString(); } int main(int argc, char *argv[]) { string s = "{\"system\": \"{\\\"version\\\":\\\"v2.6.1\\\", \\\"name\\\":\\\"value\\\"}\"}"; cout << s << endl; string str = getStringFromJson(s, "system"); cout << str << endl; cout << getStringFromJson(str, "version") << endl; return 0; }
结果:
{"system": "{\"version\":\"v2.6.1\", \"name\":\"value\"}"}
{"version":"v2.6.1", "name":"value"}
v2.6.1
第二种方式的json串,看起来太恶心了。
另外,再次强调一下,json串解析的时候,容易core dump,所以要做好异常判断,也要注意类型。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对找一找教程网的支持。如果你想了解更多相关内容请查看下面相关链接
这篇关于利用rapidjson实现解析嵌套的json的方法示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升