欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

利用rapidjson實(shí)現(xiàn)解析嵌套的json的方法示例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

利用rapidjson解析嵌套的json

看json串1:{"system":{"version":"v2.6.1", "name":"value"}}

廢話少說(shuō), 直接擼代碼:

#include <iostream>
#include <stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sstream>
// 請(qǐng)自己下載開(kāi)源的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;
}

結(jié)果:

{"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>
// 請(qǐng)自己下載開(kāi)源的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;
}

結(jié)果:

{"system": "{\"version\":\"v2.6.1\", \"name\":\"value\"}"}
{"version":"v2.6.1", "name":"value"}
v2.6.1

第二種方式的json串,看起來(lái)太惡心了。

另外,再次強(qiáng)調(diào)一下,json串解析的時(shí)候,容易core dump,所以要做好異常判斷,也要注意類型。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

上一篇:c++文件監(jiān)控之FileSystemWatcher

欄    目:C語(yǔ)言

下一篇:C++ 格式化日志輸出實(shí)現(xiàn)代碼

本文標(biāo)題:利用rapidjson實(shí)現(xiàn)解析嵌套的json的方法示例

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/362.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有