1 jsoncpp的api簡要說明
1,解析(json字符串轉為對象)
std::string strDataJson;
Json::Reader JReader;
Json::Value JObject;
if (!JReader.parse(strDataJson, JObject))
{
cerr << "parse json error." << endl;
return bSuccess;
}
2,讀取
std::string strMsg = JRec["msg"].asString();
int nRetCode = JRec["ret"]..asInt();
Json::Value JList = JRec["data"]["list"];
int nSize = JList.size();
獲取錯誤信息: JReader.getFormatedErrorMessages()
3,增加或修改
JRoot["stringdata"] = Json::Value("msg");
JRoot["intdata"] = Json::Value(10);
4,刪除
JValue.removeMember("toberemove");
5,對象轉為字符串
//輸出無格式json字符串
Json::FastWriter fast_writer;
strJRecList = fast_writer.write(JRoot);
//格式化之后的json,有回車換行符
std::string strOut = JRoot.toStyledString();
#include "json/json.h" const string fileName = "json.txt"; int main(int argc, char *argv[]) { string line; std::ifstream in(fileName.c_str()); if(!in) return 0; std::getline(in, line); Json::Reader reader; Json::Value root; if(reader.parse(line, root)) cout << "suc" << endl; else cout << "fail" << endl; cout << root["status"].asInt() << endl; cout << root["msg"].asString() << endl; cout << root["forbidReason"].asString() << endl;
Json::Value root, ipPort; string host; unsigned int port = 0; if(addrs.size() == 0) root["hosts"].append(ipPort); else { for(size_t i = 0; i < addrs.size(); i++) { if(getIpAndPort(addrs[i], host, port)) { ipPort["ip"] = host; ipPort["port"] = port; } root["hosts"].append(ipPort); } }
http://blog.csdn.net/u014489596/article/details/44920557
son是一種數據交換格式,比較適合編寫和閱讀。jsoncpp是采用c++語言編寫的用來處理json格式的第三包。直接來說明改如何使用它,本文是基於windows下的。
在github上下載jsoncpp的源代碼包:https://github.com/open-source-parsers/jsoncpp。解壓后用vs打開/makefiles/vs71/jsoncpp.sln項目,選擇lib_json項目編譯來生成lib文件,為了方便,debug和release都需要生成。
創建一個win32的空項目,將生成的lib文件包含,附加包含目錄添加源代碼中的include文件夾。后面簡單說下比較常用的幾種json處理方法。
解析json對象:
1.首先看看最簡單的一種json格式,只有鍵-值的一重嵌套:
{
“id” : 123,
"name" : "wu"
}
我們直接將上面的數據初始化到到string對象中,方便解析,后面都是如此
- std::string json = "{\"id\" : 123, \"name\" : \"wu\"}";
- Json::Reader reader;
- Json::Value root;
- std::string name;
- int id = 0;
- if (reader.parse(json, root)) // reader將Json字符串解析到root,root將包含Json里所有子元素
- {
- name = root["name"].asString();
- id = root["id"].asInt();
- }
2.再看看數組的:
[ { "id" : 1, "name" : "wu"}, {"id":2, "name" : "tan"} ]
- std::string json = "[ {\"id\" : 1, \"name\" : \"wu\"}, {\"id\" : 2, \"name\" : \"tan\"} ]";
- Json::Reader reader;
- Json::Value root;
- std::string name;
- int id = 0;
- std::map<int, std::string> mapJson;
- if (reader.parse(json, root)) // reader將Json字符串解析到root,root將包含Json里所有子元素
- {
- for (int i = 0; i < root.size(); ++i)
- {
- name = root[i]["name"].asString();
- id = root[i]["id"].asInt();
- mapJson[id] = name;
- }
- }
3.如果是這樣的數組:
{
“id” : [1, 2],
"name" : ["wu", "tan"]
}
- std::string json = "{\"id\" : [1, 2], \"name\" : [\"wu\", \"tan\"] } ";
- Json::Reader reader;
- Json::Value root;
- std::string name;
- int id = 0;
- if (reader.parse(json, root)) // reader將Json字符串解析到root,root將包含Json里所有子元素
- {
- for (int i = 0; i < root["id"].size(); ++i)
- {
- id = root["id"][i].asInt();
- }
- for (int i = 0; i < root["name"].size(); ++i)
- {
- name = root["name"][i].asString();
- }
- }
這種情況其實和上一種是類似的。
4.看看多重嵌套的情況,為了簡便,我們嵌套兩層:
{
"id" : 1,
"data" : {
"name" : "wu",
“age” : 26
}
}
- std::string json = "{\"id\" : 1, \"data\" : { \"name\" : \"wu\", \"age\" : 26 } }";
- Json::Reader reader;
- Json::Value root;
- std::string name;
- int id = 0;
- int age = 0;
- if (reader.parse(json, root)) // reader將Json字符串解析到root,root將包含Json里所有子元素
- {
- id = root["id"].asInt();
- name = root["data"]["name"].asString();
- age = root["data"]["age"].asInt();
- }
其實這種情況和第一種的類似,只是通過root["key"]取到的還是鍵值對,繼續通過key取值即可。
基本上再復雜的數據格式也是上面幾種情況的組合而已。
json對象的生成:
1.生成上面第一種情況的json格式:
- Json::Value root;
- root["id"] = 123;
- root["name"] = "wu";
- std::string json = root.toStyledString();
我們會將生成的json對象序列化到string對象中去,后面也是如此。
2.生成上面第二種情況的json:
- Json::Value root;
- for (int i = 0; i < 2; ++i)
- {
- root[i]["id"] = i + 1;
- if (0 == i)
- {
- root[i]["name"] = "wu";
- }
- else
- {
- root[i]["name"] = "tan";
- }
- }
- std::string json = root.toStyledString();
還可以這樣生成:
- Json::Value root;
- Json::Value item;
- for (int i = 0; i < 2; ++i)
- {
- item["id"] = i + 1;
- if (0 == i)
- {
- item["name"] = "wu";
- }
- else
- {
- item["name"] = "tan";
- }
- root.append(item);
- }
- std::string json = root.toStyledString();
3.生成上面第三種情況的json:
- Json::Value root;
- for (int i = 0; i < 2; ++i)
- {
- root["id"].append(i);
- if (0 == i)
- {
- root["name"].append("wu");
- }
- else
- {
- root["name"].append("tan");
- }
- }
- std::string json = root.toStyledString();
4.生成上面第四種情況的json:
- Json::Value root;
- root["id"] = 1;
- root["data"]["name"] = "wu";
- root["data"]["age"] = 26;
- std::string json = root.toStyledString();
其實解析和生成json是互逆的,只要明白這幾種情況,其他的無非是這幾種情況的各種組合,原理是一樣的。
版權聲明:本文為博主原創文章,未經博主允許不得轉載。