標籤:style blog http color 使用 os strong io
一、JSON簡介
JSON
一種輕量級的資料交換格式,易於閱讀、編寫、解析,全稱為JavsScript ObjectNotation。
JSON由兩種基本結構組成
① 名字/值 對的集合,可以理解為對象
② 值的組合, 可以理解為數組
樣本
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]}";
二、JSONCPP
1. JsonCPP簡介
jsoncpp是c++解析JSON串常用的解析庫之一。其常用的類有:
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
三、上代碼
#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;}
執行結果