sudo apt-get install scons 2. 下載jsoncpp源碼(https://github.com/open-source-parsers/jsoncpp ),解壓后得到jsoncpp-master文件夾。終端下進入jsoncpp-master目錄,然后運行如下命令即可。scons platform=linux-gcc編譯完成后,會得到libs文件夾,該文件夾下就是編譯好的動態(tài)及靜態(tài)lib庫。
下面是相應代碼示例:
#include <json/json.h>using namespace std;int main(){ // 解析json串 string message = "{ /"data/" : { /"username/" : /"test/" }, /"type/" : 6 }"; Json::Reader reader(Json::Features::strictMode()); Json::Value parseData; if (reader.parse(message.c_str(), parseData)) { int type = parseData.get("type", 0).asInt(); Json::Value frameData = parseData.get("data", 0); string username = frameData.get("username", 0).asString(); PRintf("type: %d, username: %s/n", type, username.c_str()); } // 生成json串 Json::Value data; Json::Value frame; Json::FastWriter fastWriter; frame["type"] = 66; data["username"] = "test"; frame["data"] = data; string result = fastWriter.write(frame); printf("result: %s /n", result.c_str()); return 0;} 參考資料: http://www.json.org/ https://www.ibm.com/developerworks/cn/linux/l-cn-scons/
新聞熱點
疑難解答