最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++操作.json文件的超詳細(xì)新手教程

 更新時(shí)間:2022年08月11日 17:13:09   作者:伴君  
最近因?yàn)轫?xiàng)目原因需要解析JSON格式數(shù)據(jù),所以這篇文章主要給大家介紹了關(guān)于C++操作.json文件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1.JSON簡介

1.1 什么是JSON:

JSON是一種純字符串形式的數(shù)據(jù),它本身不提供任何方法(函數(shù)),非常適合在網(wǎng)絡(luò)中進(jìn)行傳輸。JavaScript、PHP、Java、Python、C++等編程語言都內(nèi)置了處理JSON數(shù)據(jù)的方法。

JSON 是基于 JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一個(gè)子集,是一種開放的、輕量級的數(shù)據(jù)交換格式,采用獨(dú)立于編程語言的文本格式來存儲和表示數(shù)據(jù),易于程序員閱讀與編寫,同時(shí)也易于計(jì)算機(jī)解析和生成,通常用于在 Web 客戶端(瀏覽器)與 Web 服務(wù)器端之間傳遞數(shù)據(jù)。

在JSON中,使用一下兩種方式表示數(shù)據(jù):

  • object(對象):鍵/值對(名稱/值)的集合,使用花括號{}定義。在每個(gè)鍵/值對中,以鍵開頭,后跟一個(gè)冒號,最后是值。多個(gè)鍵/值對之間使用逗號,分隔,例如"name":"FilterWindow","paramtype":"0"
  • Array(數(shù)組):值的有序集合,使用方括號[]定義,數(shù)組中每個(gè)值之間使用逗號,進(jìn)行分隔。

下面展示一個(gè)簡單的JSON數(shù)據(jù):

{
	"name" : "FilterWindow",
	"paramtype" : "0",
	"range" : 
	{
		"max" : 99,
		"min" : 3
	},
	"required" : true,
	"title" : "濾波窗口大小",
	"type" : "int",
	"value" : 51
	"tempValue" : 
	[
		"不生成" : 1,
		"生成" : 0
	],
}

1.2 JSON的優(yōu)缺點(diǎn):

優(yōu)點(diǎn):

  相對于txt,word來說,json格式更加明確,獲取重要信息非常方便。

  相對于xml來說,json格式更加簡潔,存儲同樣的文件,花費(fèi)的內(nèi)存更小。

  相對于Excel來說,json更適合存儲字符類文件。Excel相當(dāng)于比較簡單的數(shù)據(jù)庫了。

  相對于數(shù)據(jù)庫來說,json更加方便,數(shù)據(jù)庫我們還需要做一些設(shè)置,安裝一些軟件。json可以直接使用。
 

缺點(diǎn):

  只有一種數(shù)字類型:JSON中只能支持IEEE-754雙精度浮點(diǎn)格式,因此我們無法使用JSON來存儲許多編程語言中多樣化的數(shù)字類型;

  沒有日期類型:在JSON中我們只能通過日期的字符串(例如:1970-01-01)或者時(shí)間戳(例如:1632366361)來表示日期;

  沒有注釋:在JSON中無法添加注釋;

  冗長:雖然JSON比XML更加簡潔,但它不是最簡潔的數(shù)據(jù)交換格式,對于數(shù)據(jù)量龐大或用途特殊的服務(wù),我們需要更加高效的數(shù)據(jù)格式。

1.3 JSON的存儲:

JSON數(shù)據(jù)可以存儲在.json格式的文件中(與.txt格式類似,都屬于純文本文件),也可以將JSON數(shù)據(jù)以字符串的形式存儲在數(shù)據(jù)庫、Cookie、Session中。要使用存儲好的JSON數(shù)據(jù)也非常簡單,不同的編程語言中提供了不同的方法來檢索和解析JSON數(shù)據(jù),今天我們的重點(diǎn)就是使用C++來操作.json文件。而C++主要使用jsoncpp這個(gè)跨平臺的開源庫來實(shí)現(xiàn)對.json文件的各類操作。

2.jsoncpp庫介紹

2.1 jsoncpp庫的配置使用:

這里我就不按照網(wǎng)上常規(guī)的方式來介紹了,常規(guī)的方法無非是關(guān)于下載jsoncpp庫,然后使用Windows編譯(vs2015),最后整理出include及l(fā)ib來使用jsoncpp庫。我這里是整理好的,是直接把include里的所有文件集合成json.h以及json.cpp。用的時(shí)候直接把這些文件包含進(jìn)自己的項(xiàng)目一起編譯即可。

2.2 jsoncpp庫內(nèi)部構(gòu)成:

jsoncpp主要包含三個(gè)class(類):Value、Reader、Writer。注意Json::Value只能處理ANSI類型的字符串,如果C++程序是用Unicode編碼的,最好加一個(gè)Adapt類來適配。

Value: 是jsoncpp中最基本、最重要的類,用于表示各種類型的對象,jsoncpp支持的對象類型可見下面的Json::ValueType枚舉值;Value類的對象代表一個(gè)JSON,既可以代表一個(gè)文檔,也可以代表文檔中一個(gè)值。如同JSON中定義的“值”一樣,Value是遞歸的。

enum ValueType
   {
      nullValue = 0, ///< 'null' value
      intValue,      ///< signed integer value
      uintValue,     ///< unsigned integer value
      realValue,     ///< double value
      stringValue,   ///< UTF-8 string value
      booleanValue,  ///< bool value
      arrayValue,    ///< array value (ordered list)
      objectValue    ///< object value (collection of name/value pairs).
   };

Value類里的函數(shù)如下所示:

1、【構(gòu)造函數(shù)】
Value( ValueType type = nullValue );
Value( Int value );
Value( UInt value );
Value( double value );
Value( const char *value );
Value( const char *beginValue, const char *endValue );
2、【拷貝構(gòu)造函數(shù)】
Value( const StaticString &value );
Value( const std::string &value );
Value( const Value &other );
3、【相同類型的比較、交換、類型的獲取】
void swap( Value &other );
ValueType type() const;
int compare( const Value &other );
4、【相應(yīng)的賦值運(yùn)算符的重載函數(shù)】
Value &operator=( const Value &other );
bool operator <( const Value &other ) const;
bool operator <=( const Value &other ) const;
bool operator >=( const Value &other ) const;
bool operator >( const Value &other ) const;
bool operator ==( const Value &other ) const;
bool operator !=( const Value &other ) const;
bool operator!() const;
Value &operator[]( UInt index );
const Value &operator[]( UInt index ) const;
5、【將Value對象進(jìn)行相應(yīng)的類型轉(zhuǎn)換】
const char *asCString() const;
std::string asString() const; 
const char *asCString() const;
std::string asString() const;
Int asInt() const;
UInt asUInt() const;
double asDouble() const;
6、【相應(yīng)的判斷函數(shù)】
bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isUInt() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;
bool isConvertibleTo( ValueType other ) const;
bool isValidIndex( UInt index ) const;
bool isMember( const char *key ) const;
bool isMember( const std::string &key ) const;
7、【清除和擴(kuò)容函數(shù)】
void clear();
void resize( UInt size );
8、【獲取滿足相應(yīng)條件的Value】
Value get( UInt index, const Value &defaultValue ) const;
Value get( const std::string &key,const Value &defaultValue ) const;
Members getMemberNames() const;
9、【刪除滿足相應(yīng)條件的Value】
Value removeMember( const char* key );
Value removeMember( const std::string &key );
10、【】
void setComment( const char *comment,CommentPlacement placement );
void setComment( const std::string &comment,CommentPlacement placement );
bool hasComment( CommentPlacement placement ) const;
std::string getComment( CommentPlacement placement ) const;
std::string toStyledString()const;

Reader :是用于讀取的,確切說是用于將字符串轉(zhuǎn)換為Json::Value對象的。

1、【構(gòu)造函數(shù)】
Reader();
2、【拷貝構(gòu)造函數(shù)】
Reader( const Features &features );
3、【將字符串或者輸入流轉(zhuǎn)換為JSON的Value對象】【下邊是相應(yīng)的parse的重載函數(shù)】
bool parse( const std::string &document, Value &root,bool collectComments = true );
bool parse(const char *beginDoc, const char *endDoc, Value &root,bool collectComments = true)    
bool parse( std::istream &is,Value &root,bool collectComments = true );
4、【】
std::string getFormatedErrorMessages() const;

Writer :是一個(gè)純虛類,并不能直接使用。在此我們使用Json::Writer的子類(派生類):Json::FastWriter、Json::StyledWriter、Json::StyledStreamWriter。顧名思義,用Json::FastWriter來處理json應(yīng)該是最快的;負(fù)責(zé)將內(nèi)存中的Value對象轉(zhuǎn)換成JSON文檔,輸出到文件或者字符串中。

 1、【FastWriter】
FastWriter();
virtual ~FastWriter(){}
void enableYAMLCompatibility();
virtual std::string write( const Value &root );
2、【StyledWriter】
StyledWriter();
virtual ~StyledWriter(){}
virtual std::string write( const Value &root );

3.json文件讀取(例)

3.1 json文件:

{
  "face": [
    {
      "attribute": {
        "age": {
          "range": 5,
          "value": 35
        },
        "gender": {
          "confidence": 99.9995,
          "value": "Male"
        },
        "glass": {
          "confidence": 98.8995,
          "value": "None"
        },
        "pose": {
          "pitch_angle": {
            "value": -0.000006
          },
          "roll_angle": {
            "value": 5.32508
          },
          "yaw_angle": {
            "value": -22.432627
          }
        },
        "race": {
          "confidence": 98.62100000000001,
          "value": "Asian"
        },
        "smiling": {
          "value": 97.3715
        }
      },
      "face_id": "2f60c56506b691c0384e2694fed1c819",
      "position": {
        "center": {
          "x": 51.463415,
          "y": 25.121951
        },
        "eye_left": {
          "x": 46.197561,
          "y": 20.161
        },
        "eye_right": {
          "x": 56.724146,
          "y": 21.142171
        },
        "height": 22.926829,
        "mouth_left": {
          "x": 45.610732,
          "y": 30.399024
        },
        "mouth_right": {
          "x": 56.01561,
          "y": 31.734146
        },
        "nose": {
          "x": 49.063659,
          "y": 27.171951
        },
        "width": 22.926829
      },
      "tag": ""
    }
  ],
  "img_height": 410,
  "img_id": "84c20011223acd4efa0b5aa13fc2146d",
  "img_width": 410,
  "session_id": "42c5db376fdc4da9855d0135b5e414e2",
  "url": "http://www.faceplusplus.com.cn/wp-content/themes/faceplusplus/assets/img/demo/16.jpg?v=2"
}

3.2 源碼:

#include<iostream>
#include<fstream>
#include<assert.h>
#include "json.h"

using namespace std;

int main()
{
	Json::Reader reader;
	Json::Value root;

	ifstream is;  

	is.open("face.json", ios::binary);

	if (reader.parse(is, root))
	{
		Json::StyledWriter sw;     //縮進(jìn)輸出
		cout << "縮進(jìn)輸出" << endl;
		cout << sw.write(root) << endl << endl;
	}
	return 0;
}

3.3 結(jié)果圖:

總結(jié) 

到此這篇關(guān)于C++操作.json文件的文章就介紹到這了,更多相關(guān)C++操作.json文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++多重繼承及多態(tài)性原理實(shí)例詳解

    C++多重繼承及多態(tài)性原理實(shí)例詳解

    這篇文章主要介紹了C++多重繼承及多態(tài)性原理實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Cocos2d-x觸摸事件實(shí)例

    Cocos2d-x觸摸事件實(shí)例

    這篇文章主要介紹了Cocos2d-x觸摸事件實(shí)例,本文代碼中包含大量注釋來說明Cocos2d-x中的觸摸事件使用示例,需要的朋友可以參考下
    2014-09-09
  • C++ 函數(shù)指針詳細(xì)總結(jié)

    C++ 函數(shù)指針詳細(xì)總結(jié)

    這篇文章主要介紹了C++ 函數(shù)指針內(nèi)容,下面文章圍繞C++ 函數(shù)指針的相關(guān)資料展開詳細(xì)內(nèi)容,包括函數(shù)指針的進(jìn)階內(nèi)容,需要的朋友可以參考一下,希望對大家有所幫助
    2021-11-11
  • C語言實(shí)現(xiàn)員工工資管理系統(tǒng)

    C語言實(shí)現(xiàn)員工工資管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)員工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C/C++?判斷計(jì)算機(jī)存儲器字節(jié)序(端序)的幾種方式

    C/C++?判斷計(jì)算機(jī)存儲器字節(jié)序(端序)的幾種方式

    字節(jié)序是計(jì)算機(jī)存儲數(shù)據(jù)的格式,主存儲器(主存)的字節(jié)序?qū)Τ绦虻囊浦残院图嫒菪灾陵P(guān)重要,利用聯(lián)合體、指針、位移和掩碼等方法可以檢測和處理字節(jié)序問題,對于內(nèi)存數(shù)據(jù)操作重要,也關(guān)系到跨平臺和網(wǎng)絡(luò)通信的數(shù)據(jù)處理
    2024-10-10
  • C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn)詳解

    C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn)詳解

    這篇文章主要介紹了C標(biāo)準(zhǔn)庫<assert.h>的實(shí)現(xiàn),主要包括了<assert.h>的基本概念、實(shí)現(xiàn)及用法等,需要的朋友可以參考下
    2014-09-09
  • C語言中傳值與傳指針的介紹與區(qū)別

    C語言中傳值與傳指針的介紹與區(qū)別

    這篇文章主要給大家介紹了關(guān)于C語言中傳值與傳指針的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C語言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • C++之友元:友元函數(shù)和友元類詳解

    C++之友元:友元函數(shù)和友元類詳解

    友元是一種允許非類成員函數(shù)訪問類的非公有成員的一種機(jī)制??梢园岩粋€(gè)函數(shù)指定為類的友元,也可以把整個(gè)類指定為另一個(gè)類的友元
    2013-09-09
  • C++動(dòng)態(tài)內(nèi)存分配超詳細(xì)講解

    C++動(dòng)態(tài)內(nèi)存分配超詳細(xì)講解

    給數(shù)組分配多大的空間?你是否和初學(xué)C時(shí)的我一樣,有過這樣的疑問。這一期就來聊一聊動(dòng)態(tài)內(nèi)存的分配,讀完這篇文章,你可能對內(nèi)存的分配有一個(gè)更好的理解
    2022-08-08
  • QT調(diào)用vs2019生成的c++動(dòng)態(tài)庫的方法實(shí)現(xiàn)

    QT調(diào)用vs2019生成的c++動(dòng)態(tài)庫的方法實(shí)現(xiàn)

    本文主要介紹了QT調(diào)用vs2019生成的c++動(dòng)態(tài)庫的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06

最新評論

自治县| 山西省| 大兴区| 孝感市| 屯昌县| 郸城县| 盖州市| 巴楚县| 晴隆县| 南丰县| 七台河市| 克什克腾旗| 遂宁市| 海林市| 榆中县| 崇左市| 九台市| 加查县| 邹平县| 元氏县| 嘉鱼县| 策勒县| 新密市| 金塔县| 抚州市| 镇远县| 平山县| 海门市| 双牌县| 绿春县| 丰城市| 保靖县| 弥渡县| 台北县| 武宁县| 工布江达县| 汝阳县| 昭平县| 弥勒县| 天水市| 凉山|