当前位置 : 主页 > 编程语言 > c语言 >

rapidjson: 读取复杂的json串

来源:互联网 收集:自由互联 发布时间:2023-08-28
简述 rapidjson: 读取复杂的json串 内容 如下json为例,使用rapidjson解析 {"data": {"security_token": "CAIS+QJ1q6Ft5B2yfSj...Hvo2LHm","access_key_id": "STS.NTAgDURZo3op8zMRrfYvPG6yF","access_key_secret": "HgeVRTDRH2TpKSDibH87

简述

rapidjson: 读取复杂的json串

内容

如下json为例,使用rapidjson解析

{
"data": {
	"security_token": "CAIS+QJ1q6Ft5B2yfSj...Hvo2LHm",
	"access_key_id": "STS.NTAgDURZo3op8zMRrfYvPG6yF",
	"access_key_secret": "HgeVRTDRH2TpKSDibH87wE2JyfXcJiDP1n3DagMTkBJB",
	"expiration": 1599035693,
	"resources": [
								{"path": "dog-test/9062e25d-86c0-46d2-a2e5-7c003e461118/pictures/",
			"endpoint": "https://oss-cn-shenzhen.aliyuncs.com"}
								]
			}
}

以上json串包含:Object,Array,Key-Value,嵌套Object,嵌套Array等基本结构。

解析代码如下

#include "rapidjson/document.h"

  
// 解析OSS STS response中的json串

// pStrRespData为待解析json串

bool parse_osssts_json_data(myyk::SOSSInfo &struSTSResponse, string *pStrRespData)

{

    struSTSResponse.result = false;

  
    rapidjson::Document doc;

    doc.Parse((*pStrRespData).c_str());

  
    if(!doc.IsObject())

    {

        LogError(string("Authorization response format isn't json format: ") + *pStrRespData);

        return false;

    }

  
    // 鉴权成功

    if(doc.HasMember("data"))

    {

        rapidjson::Document::Object dataObj = doc["data"].GetObject();

  
        struSTSResponse.mStrSecurityToken = dataObj["security_token"].GetString();

        struSTSResponse.mStrAccessKeyId = dataObj["access_key_id"].GetString();

        struSTSResponse.mStrAccessKeySecret = dataObj["access_key_secret"].GetString();

        struSTSResponse.expiresTime = dataObj["expiration"].GetInt64();       // 授权到期时间点

  
        rapidjson::Document::Array array = dataObj["resources"].GetArray();  // json array type

        if(array.Size() < 1)

        {

            LogInfo("OSS STS response json parse failed, 'resources' array size < 1");

            return false;

        }

  
        rapidjson::Document::Object resObj = array[0].GetObject();

        string path = resObj["path"].GetString();    // <bucketName>/<savePath>, aiot-rcb/9062e25d-86c0-46d2-a2e5-7c003e461118/pictures/

        struSTSResponse.mStrEndpoint = resObj["endpoint"].GetString();    // endPoint, https://oss-cn-shenzhen.aliyuncs.com

        bool bRet =  [&]()->bool {

                auto index = path.find_first_of('/');

                if( string::npos == index ){

                    return false;

                }

  
                struSTSResponse.mStrBucketName = path.substr(0, index);

                struSTSResponse.mOSSSavePath = path.substr(index+1);

                return true;

            }();

  
        if(!bRet)

        {

            LogError("OSS STS response json parse failed, 'bucket name' not found");

            return false;

        }

  
        LogInfo("OSS STS response json parse success.");

        struSTSResponse.result = true;

        return true;

    }

  
    return false;

}
上一篇:XJOI1级19段 c++ 爱丽丝的卡片
下一篇:没有了
网友评论