jsoncpp官方教程 C json解析?
C json解析?代碼示例:#include <iostream>#include <string>#include <jsoncpp/json/json.h>using n
C json解析?
代碼示例:
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
using namespace std
int main()
{
string strJsonContent = "{"role_id": 1,"occupation": "paladin","camp": "alliance"}"
int nRoleDd = 0
string strOccupation = ""
string strCamp = ""
Json::Reader reader
Json::Value root
if (reader.parse(strJsonContent, root))
{
nRoleDd = root["role_id"].asInt()
strOccupation = root["occupation"].asString()
strCamp = root["camp"].asString()
}
cout << "role_id is: " << nRoleDd << endl
cout << "occupation is: " << strOccupation << endl
cout << "camp is: " << strCamp << endl
return 0
}
誰(shuí)給推薦一個(gè)C 的JSON庫(kù)?
jsoncpp、還有cJSON,推薦cJSON這個(gè)輕量級(jí)的JSON庫(kù),它使用起來(lái)非常簡(jiǎn)單,只需要把cJSON.c和cJSON.h兩個(gè)文件復(fù)制到你的項(xiàng)目中就可以了,使用時(shí)#include "cJSON.h"。cJSON可以在github上下載,github上還有一段簡(jiǎn)單的使用說(shuō)明,cJSON庫(kù)可以解析JSON,也可以生成JSON文件,用cJSON一般只用寫4、5行代碼(所以說(shuō)它的輕量級(jí)的)。
jsoncpp的內(nèi)部排序怎么去掉?json?
參考方法就是先把文件讀出來(lái),把不要的數(shù)組元素刪了后再寫回去;參考代碼如下:// std::string jsonPath // json文件路徑Json::Reader reader Json::Value root ifstream is is.open (jsonPath.c_str(), std::ios::binary ) if (reader.parse(is, root)) { std::string codeJson::Value valueint size = root.size()for (int i = 0 i < size i ){ if(條件){value[i] = root[i]}}is.close() Json::FastWriter writerstd::string json_append_file = writer.write(value)std::ofstream ofsofs.open(jsonPath.c_str())ofs << json_append_fileofs.close()}