json介绍:
json分为:
json对象:
1 2 3 4 5 6 7 8 9 10 { "class":31, "count":51, "master":"yalong", "banzhuren":"chenlaoshi", "like":["dancing", "sing", "drinking"], "hometown":{"hebei":"baoding", "henan":"xinyang"} }
json数组:
1 [10, 10.5, ["xiaohu", "liming", 10], {"name":"xiaohu", "age":32, "sex":"man"}]
1 json对象格式的数据都是以key:value的形式存在的, 其中key值是字符串形式的.
2 在json对象中, value值可以是子对象, 也可以是数组
3 在json格式的文件中, 只能是json对象或者是json数组
4 在json数组中, 数据的类型不定, 可以是double int string 子对象, 子数组.
头文件
1 2 3 4 5 6 #include <QJsonDocument> #include <QJsonArray> #include <QJsonObject> #include <QByteArray> #include <QJsonValue> #include <QFile>
json对象操作
创建json对象
给json对象插入值
1 iterator QJsonObject::insert(const QString &key, const QJsonValue &value)
json数组操作
创建json数组
给json数组插入值
1 void QJsonArray::append(const QJsonValue &value)
将QJsonObject 或QJsonArray转换为 QJsonDocument
1 2 3 JsonDocument::QJsonDocument(const QJsonObject &object) //将json对象转换为QJsonDocument JsonDocument::QJsonDocument(const QJsonArray &array) //将json数组转换为QJsonDocument
将QJsonDocument对象转换为QByteArray对象
1 QByteArray QJsonDocument::toJson(JsonFormat format = Indented) const
操作文件
创建文件对象
1 2 3 QFile::QFile(const QString &name) QFile file(fileName);
打开文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 virtual bool open(OpenMode mode) QIODevice::NotOpen 0x0000 The device is not open. QIODevice::ReadOnly 0x0001 The device is open for reading. QIODevice::WriteOnly 0x0002 The device is open for writing. Note that this mode implies Truncate. QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing. QIODevice::Append 0x0004 The device is opened in append mode so that all data is written to the end of the file. QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost. QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32. QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed.
读文件
1 2 3 QByteArray QIODevice::readAll() //一次性读完 QByteArray byteArray = file.readAll();
将QByteArray类对象转换为QJsonDocument
1 2 3 QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = Q_NULLPTR) QJsonDocument jsonDoc = QJsonDocument::fromJson(byteArray);
判断是不是对象
1 bool QJsonDocument::isObject() const
将QJsonDocument转换为QJsonObject
1 QJsonObject QJsonDocument::object() const
将QJsonDocument转换为QJsonArray
1 QJsonArray QJsonDocument::array() const
获取对象中所有的key值
1 QStringList QJsonObject::keys() const
返回key的数量
1 int QJsonObject::size() const
根据key值获取value值
1 2 QJsonValue QJsonObject::value(const QString &key) const QJsonValue QJsonObject::value(QLatin1String key) const
判断value值的类型
1 2 3 4 5 6 7 bool isArray() const bool isBool() const bool isDouble() const bool isNull() const bool isObject() const bool isString() const bool isUndefined() const
将value转换到对应类型的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 QJsonArray toArray(const QJsonArray &defaultValue) const //转换到数组 QJsonArray toArray() const //转换到数组 bool toBool(bool defaultValue = false) const //转换到布尔 double toDouble(double defaultValue = 0) const //转换到到double int toInt(int defaultValue = 0) const //转换到整型 QJsonObject toObject(const QJsonObject &defaultValue) const //转换到对象 QJsonObject toObject() const //转换到对象 QString toString() const //转换到字符串 QString toString(const QString &defaultValue) const //转换到字符串 QVariant toVariant() const
将QJsonDocument转换为const char *
1 const char *QJsonDocument::rawData(int *size) const
将const char * 转换为QJsonDocument
1 fromRawData(const char *data, int size, DataValidation validation = Validate)
例子
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 #include <QCoreApplication> #include <QJsonDocument> #include <QJsonArray> #include <QJsonObject> #include <QByteArray> #include <QJsonValue> #include <QFile> #include <QDebug> void writeJsonToFile (char *fileName) { QJsonArray array ; array .append(10 ); array .append("jinyanlong" ); array .append(true ); QJsonArray subArray; subArray.append("english" ); subArray.append("chinese" ); subArray.append("history" ); array .append(subArray); QJsonObject subObj; subObj.insert("mother" , "xiaoling" ); subObj.insert("father" , "xiaotong" ); subObj.insert("sister" , "damiao" ); array .append(subObj); QJsonDocument jsonDoc (array ) ; QByteArray byteArray = jsonDoc.toJson(); QFile file (fileName) ; file.open(QIODevice::WriteOnly); file.write(byteArray); file.close(); return ; } void readJsonFromFile (char *fileName) { QFile file; file.setFileName(fileName); file.open(QIODevice::ReadOnly); QByteArray byteArray = file.readAll(); file.close(); QJsonDocument jsonDoc = QJsonDocument::fromJson(byteArray); if (jsonDoc.isObject()) { QJsonObject jsonObj = jsonDoc.object(); QStringList keys = jsonObj.keys(); for (int i=0 ; i<keys.size(); i++) { QString key = keys.at(i); QJsonValue jsonValue = jsonObj.value(key); if (jsonValue.isString()) { qDebug() << key << ":" << jsonValue.toString(); } else if (jsonValue.isDouble()) { qDebug() << key << ":" << jsonValue.toInt(); } else if (jsonValue.isBool()) { qDebug() << key << ":" << jsonValue.toBool(); } else if (jsonValue.isObject()) { QJsonObject obj = jsonValue.toObject(); QStringList subKeys = obj.keys(); qDebug() << key << ":{" ; for (int k=0 ; k<subKeys.size(); k++) { QString subkey = subKeys[k]; QJsonValue subJsonValue = obj.value(subkey); qDebug() << " " << subJsonValue.toString(); } qDebug() << "}" ; } else if (jsonValue.isArray()) { qDebug() << key << ":[" ; QJsonArray arr = jsonValue.toArray(); for (int j=0 ; j<arr.size(); j++) { QJsonValue va = arr[j]; if (va.isString()) { qDebug() << " " << va.toString(); } } qDebug() <<"]" ; } } } else if (jsonDoc.isArray()) { QJsonArray array = jsonDoc.array (); for (int i=0 ; i<array .size(); i++) { QJsonValue value = array [i]; if (value.isString()) { qDebug() << value.toString(); } else if (value.isDouble()) { qDebug() << value.toInt(); } else if (value.isBool()) { qDebug() << value.toBool(); } else if (value.isArray()) { qDebug() << "[" ; QJsonArray subArray = value.toArray(); for (int j=0 ; j<subArray.size(); j++) { qDebug() << " " << subArray[j].toString(); } qDebug() << "]" ; } else if (value.isObject()) { qDebug() << "{" ; QJsonObject subObj = value.toObject(); QStringList subKeys = subObj.keys(); for (int k=0 ; k<subKeys.size(); k++) { QString subkey = subKeys[k]; QJsonValue value = subObj[subkey]; if (value.isString()) { qDebug() << " " << value.toString(); } } qDebug() << "}" ; } } } } int main (int argc, char *argv[]) { QCoreApplication a (argc, argv) ; writeJsonToFile("test.json" ); readJsonFromFile("test.json" ); return a.exec(); }