利用rapidjson解析嵌套的json

972 篇文章 329 订阅
166 篇文章 14 订阅

       看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,  所以要做好异常判断, 也要注意类型。

 


 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
RapidJSON是一个快速的C++ JSON解析器/生成器,支持SAX和DOM风格API。它支持多种平台,并且易于集成到现有项目中。 下面是使用RapidJSON读取嵌套JSON的示例代码: ```c++ #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/stringbuffer.h" #include <iostream> #include <string> using namespace rapidjson; using namespace std; int main() { // 假设我们有以下嵌套json数据 string jsonStr = "{\"name\": \"John\", \"age\": 30, \"address\": {\"street\": \"123 Main St\", \"city\": \"Anytown\", \"state\": \"CA\", \"zip\": \"12345\"}, \"phone_numbers\": [{\"type\": \"home\", \"number\": \"555-1234\"}, {\"type\": \"work\", \"number\": \"555-5678\"}]}"; // 解析JSON字符串 Document d; d.Parse(jsonStr.c_str()); // 访问JSON数据 cout << "Name: " << d["name"].GetString() << endl; cout << "City: " << d["address"]["city"].GetString() << endl; // 遍历JSON数组 const Value& phoneNumbers = d["phone_numbers"]; for (SizeType i = 0; i < phoneNumbers.Size(); i++) { const Value& phoneNumber = phoneNumbers[i]; cout << "Phone Type: " << phoneNumber["type"].GetString() << endl; cout << "Phone Number: " << phoneNumber["number"].GetString() << endl; } return 0; } ``` 输出: ``` Name: John City: Anytown Phone Type: home Phone Number: 555-1234 Phone Type: work Phone Number: 555-5678 ``` 在这个示例中,我们使用RapidJSON的Document类解析JSON字符串,并使用键值对来访问嵌套JSON数据。我们还演示了如何遍历JSON数组。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值