CJson
之前有介绍过json,这里再次引用一下json的定义:
JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
json是与编程语言无关的一种数据储存格式,不同的语言都有成熟的json库去生成和解析json,我有介绍过两种json库:
CJson是封装了Json for Modern C++的对象,对外提供一些简单的接口
实现
关于Json for Modern C++可以查看其介绍,这里直接放实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #include "cjson.h"
CJson::CJson() {
}
CJson::CJson(std::string str) { mJson = nlohmann::json::parse(str); }
CJson::CJson(const char* str) { std::string json_str = str; mJson = nlohmann::json::parse(json_str); }
CJson::CJson(nlohmann::json object) { mJson = object; }
CJson::~CJson() {
}
nlohmann::json CJson::getObjectByKey(string key) { nlohmann::json object_json; try { object_json = mJson[key]; } catch(const std::exception& e) { } return object_json; }
int CJson::getValueByKey(string key, string &value) { int ret = -1; try { value = mJson[key]; ret = 0; } catch(const std::exception& e) { }
return ret; }
int CJson::getValueByKey(string key, int *value) { int ret = -1; try { *value = mJson[key]; ret = 0; } catch(const std::exception& e) { }
return ret; }
bool CJson::isExistKey(std::string key) { if (mJson.contains(key)) { return true; }
return false; }
bool CJson::isExistKey(std::string key, nlohmann::json json) { if (json.contains(key)) { return true; }
return false; }
int CJson::setValueByKey(string key, string value) { if (!isExistKey(key)) { return -1; }
mJson[key] = value; return 0; }
int CJson::setValueByKey(string key, const char* value) { string set_value = value;
return setValueByKey(key, set_value); }
int CJson::setValueByKey(string key, int value) { if (!isExistKey(key)) { return -1; }
mJson[key] = value; return 0; }
void CJson::insertKeyValue(string key, string value) { mJson[key] = value; }
void CJson::insertKeyValue(string key, const char* value) { string set_value = value; mJson[key] = set_value; }
void CJson::insertKeyValue(string key, int value) { mJson[key] = value; }
void CJson::insertKeyObject(string key, nlohmann::json object) { mJson[key] = object; }
nlohmann::json CJson::getJson() { return mJson; }
string CJson::getJsonToString() { string sJson; try { sJson = mJson.dump(); } catch (const std::exception& ex) { printf("json to string error :%s", ex.what()); }
return sJson; }
int CJson::getArraySize() { if (mJson.type() == nlohmann::json::value_t::array) { return mJson.size(); }
return -1; }
int CJson::getArrayValueOfIndex(int index, nlohmann::json &value) { if (mJson.type() == nlohmann::json::value_t::array) { if (mJson.size() >= index) return -1; value = mJson.at(index); return 0; }
return -1; }
int CJson::insertArrayValueByIndex(int index, nlohmann::json value) { if (mJson.type() == nlohmann::json::value_t::array) { if (mJson.size() > index) return -1; mJson[index] = value; return 0; }
return -1; }
|