【JSONCpp】簡介及demo

1、JSON簡介ios

JSONc++

一種輕量級的數據交換格式,易於閱讀、編寫、解析,全稱爲JavsScript ObjectNotation。json

JSON由兩種基本結構組成數組

①   名字/值 對的集合,能夠理解爲對象app

②   值的組合, 能夠理解爲數組函數

 

示例ui

string strTemp = "{ \"name\" : \"cuihao\" ," \ " \"age\" : 28 }"; string strRoot = "{ \"key_string\" : \"value_string\", " \ " \"key_number\" : 28, " \ " \"key_boolean\" : false, " \ " \"key_double\" : 12.345, " \ " \"key_object\" : json_temp, " \ " \"key_array\" : [\"array_string1\", \"array_string2\", 12345]}";

 

 

 

 

2、JSONCPPspa

1.     JsonCPP簡介debug

     jsoncpp是c++解析JSON串經常使用的解析庫之一。其經常使用的類有:code

a)     Json::Value    能夠表示裏全部的類型,好比int,string,object,array等,其支持的類型能夠參考Json:ValueType中的值。

b)     Json::Reader  將json文件流或字符串解析到Json::Value,主要函數有Parse。

c)     Json::Writer   與Json::Reader相反,將Json::Value轉化成字符串流,注意它的兩個子類:Json::FastWriter和Json::StyleWriter,分別輸出不帶格式的json和帶格式的json。

d)     Json::Value::Members主要用於以STL風格解析JSON數組。看過源代碼的人已知道,Members實際上是typedefvector而已。

 

 

在VC中使用JSONCPP

下載源碼後,進入D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\makefiles\vs71相似目錄用VS打開.sln文件進行編譯。

注意:

Debug下能夠直接編譯

Release下:將lib_json項目屬性:配置屬性—C/C++--輸出文件:彙編輸出選擇【無列表】,應爲爲No List。

 

使用JSONCPP時,添加

頭文件目錄:D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include

庫目錄:

Debug: D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\debug\lib_json

#pragma  commebt(lib, 「json_vc71_libmtd.lib」) 對應/MDd

 

Release: D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\release\lib_json

#pragma  commebt(lib, 「json_vc71_libmt.lib」)  對應/MT

 

 

 

3、上代碼

 

#include "stdafx.h" #include <iostream> #include <string> #include <json\json.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { string strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }"; //char* strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }";  Json::Reader reader; Json::Value value; bool bRet = reader.parse(strStudent, value); if (false == bRet) { cerr << endl << "read value error \n"; return -1; } cout << value["name"].asString()<<endl; cout << value["age"].asInt()<<endl; cout << value["major"].asString()<<endl; cout << endl; Json::Value json_temp; json_temp["name"] = Json::Value("cuihao"); json_temp["age"] = Json::Value(28); Json::Value root; root["key_string"] = Json::Value("value_string"); root["key_number"] = Json::Value(12345); root["key_boolean"] = Json::Value(true); root["key_double"] = Json::Value(12.345); root["key_object"] = json_temp; root["key_array"].append("array_string1"); root["key_array"].append("array_string2"); root["key_array"].append(12345); Json::ValueType type = root.type(); Json::Value arrValue = root["key_array"]; for (Json::Value::UInt i = 0; i < arrValue.size(); ++ i) { if (arrValue[i].isString()) cout << arrValue[i].asString(); else if (arrValue[i].isInt()) cout << arrValue[i].asInt(); cout << endl; } cout << endl << "----------------------------------- " <<endl; string strTemp = "{ \"name\" : \"cuihao\" ," \ " \"age\" : 28 }"; string strRoot = "{ \"key_string\" : \"value_string\", " \ " \"key_number\" : 28, " \ " \"key_boolean\" : false, " \ " \"key_double\" : 12.345, " \ " \"key_object\" : json_temp, " \ " \"key_array\" : [\"array_string1\", \"array_string2\", 12345]}"; Json::StyledWriter writer; cout << endl << writer.write(root) << endl; cout << endl << "----------------------------"<<endl; Json::Value::Members members = root.getMemberNames(); for(Json::Value::Members::const_iterator iter = members.begin(); iter != members.end(); ++ iter) { string strName = *iter; if (root[strName].isInt()) cout << root[strName].asInt(); else if (root[strName].isString()) cout << root[strName].asString(); else if (root[strName].isDouble()) cout << root[strName].asDouble(); else if(root[strName].isBool()) cout << root[strName].asBool(); else if(root[strName].isArray()) { for(Json::Value::ArrayIndex i = 0; i < root[strName].size(); ++ i) { if(root[strName][i].isInt()) cout << root[strName][i].asInt(); else if(root[strName][i].isString()) cout << root[strName][i].asString(); else cout << "others"; cout << endl; } } else if (root[strName].isObject()) { Json::Value::Members mbs = root[strName].getMemberNames(); for (Json::Value::Members::const_iterator iter2 = mbs.begin(); iter2 != mbs.end(); ++ iter2) { string strName2 = *iter2; if(root[strName][strName2].isInt()) cout << root[strName][strName2].asInt(); else if(root[strName][strName2].isString()) cout << root[strName][strName2].asString(); else cout << "others"; cout << endl; } //for } //else if else cout << "others"; cout << endl; } return 0; }

 

 

 

執行結果

相關文章
相關標籤/搜索