整合自網路python
1、安裝的方法linux
一、安裝 sconsjson
下載地址:http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.tar.gz/download數組
百度網盤:連接:https://pan.baidu.com/s/1tW57c9s3iCeoDi4OIDyEPQ 密碼:2wd5spa
解壓:tar -zvxf scons-2.1.0.tar.gz.net
進入到解壓目錄scons-2.1.0 執行命令:code
sudo python setup.py installorm
二、安裝 Jsoncppblog
下載地址:http://sourceforge.net/projects/jsoncpp/接口
解壓:tar -zvxf jsoncpp-src-0.5.0.tar.gz
進入到jsoncpp解壓目錄下,執行命令:
sudo scons platform=linux-gcc
將/jsoncpp-src-0.5.0/include/目錄下的json文件夾拷貝到 /usr/local/include/
將jsoncpp-src-0.5.0/libs/linux-gcc-4.9.1/目錄下的libjson_linux-gcc-4.9.1_libmt.a 拷貝到 /usr/local/lib/ 下,併爲了方便使用,將其重命名爲libjson.a
2、使用方法
一、使用 parse 接口進行轉化
Json::Reader *pJsonParser = new Json::Reader(); string strJson = "1111 {}"; //不合法json Json::Value tempVal; if(!pJsonParser->parse(strJson, tempVal)){ cout << "parse error" << endl; return -1; } string name = tempVal["name"].asString();
二、數組解析
Json::Reader *pJsonParser = new Json::Reader(); string strJson = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":\"24\",\"friends\":[{\"name\":\"chen\',\'sex\':\'男\"},{\"name\":\"li\",\"sex\":\"女\"}]}"; Json::Value tempVal; if(!pJsonParser->parse(strJson, tempVal)){ return -1; }
Json::Value friends = tempVal["friends"];
for(int i = 0;i < friends.size();i++){
cout << friends[i]["name"].asString() << endl;
}
三、先進行類型判斷
if(tempVal["name"].isInt()){ int name = tempVal["name"].asInt(); }