/*****Damo1*****/
{
"w":"1",
"ww":{
"w":"1",
"ww":"2"
}
}
QJsonObject obj1; obj1.insert("w", ""); obj1.insert("ww", "");; QJsonObject json; json.insert("w", ""); json.insert("ww", QJsonValue(obj1)); // 构建 Json 文档 QJsonDocument document; document.setObject(json); QByteArray byteArray = document.toJson(QJsonDocument::Compact); QString strJson(byteArray); qDebug() << strJson; QJsonParseError jsonError; QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); // 转化为 JSON 文档 if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) { // 解析未发生错误 if (doucment.isObject()) { // JSON 文档为对象 QJsonObject object = doucment.object(); // 转化为对象 if(object.contains("w")){ QJsonValue value = object.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString(); } } if(object.contains("ww")){ QJsonValue value = object.value("ww"); if(value.isObject()){ QJsonObject obj = value.toObject(); if(obj.contains("w")){ QJsonValue value = obj.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString(); } } if(obj.contains("ww")){ QJsonValue value = obj.value("ww"); if(value.isString()){ qDebug() << "ww :" << value.toString(); } } } } } }
/*****Damo2*****/
{
"w":"1",
“ww":[{
"w":"1",
"ww":"2"
}]
}
QJsonObject obj1; obj1.insert("w", "1"); obj1.insert("ww", "2"); QJsonArray array; array.append(QJsonValue(obj1)); QJsonObject json; json.insert("w", "1"); json.insert("ww", QJsonValue(array)); // 构建 Json 文档 QJsonDocument document; document.setObject(json); QByteArray byteArray = document.toJson(QJsonDocument::Compact); QString strJson(byteArray); qDebug() << strJson; QJsonParseError jsonError; QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)){ if(doucment.isObject()){ QJsonObject object = doucment.object(); if(object.contains("w")){ QJsonValue value = object.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(object.contains("ww")){ QJsonValue value = object.value("ww"); if(value.isArray()){ QJsonArray array = value.toArray(); int nSize = array.size(); for(int i = 0; i < nSize; i++){ QJsonValue object = array.at(i); if(object.isObject()){ QJsonObject obj = object.toObject(); if(obj.contains("w")){ QJsonValue value = obj.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(obj.contains("ww")){ QJsonValue value = obj.value("ww"); if(value.isString()){ qDebug() << "ww :" << value.toString() << endl; } } } } } } } }/*****Damo3*****/
{
"w":"1",
"ww":[
[
"w":"1",
"ww":"2"
]
]
}
QJsonObject objA; objA.insert("w", "1"); objA.insert("ww", "2"); QJsonArray arrayA; arrayA.append(QJsonValue(objA)); QJsonArray arrayB; arrayB.append(QJsonValue(arrayA)); QJsonObject json; json.insert("w", "1"); json.insert("ww", QJsonValue(arrayB)); // 构建 Json 文档 QJsonDocument document; document.setObject(json); QByteArray byteArray = document.toJson(QJsonDocument::Compact); QString strJson(byteArray); qDebug() << strJson; QJsonParseError jsonError; QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)){ if(doucment.isObject()){ QJsonObject object = doucment.object(); if(object.contains("w")){ QJsonValue value = object.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(object.contains("ww")){ QJsonValue value = object.value("ww"); if(value.isArray()){ QJsonArray array = value.toArray(); int nSize = array.size(); for(int i = 0; i < nSize; i++){ QJsonValue object = array.at(i); if(object.isArray()){ QJsonArray array = object.toArray(); int nSize = array.size(); for(int i = 0; i < nSize; i++){ QJsonValue obj1 = array.at(i); if(obj1.isObject()){ QJsonObject obj2 = obj1.toObject(); if(obj2.contains("w")){ QJsonValue value = obj2.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(obj2.contains("ww")){ QJsonValue value = obj2.value("ww"); if(value.isString()){ qDebug() << "ww :" << value.toString() << endl; } } } } } } } } } }
/*****Damo4*****/
{
"w":"1",
"ww":[{
"w":"1",
"ww":[{
"w":"1",
"ww":"2"
}]
}]
}
QJsonObject objA; objA.insert("w", "1"); objA.insert("ww", "2"); QJsonArray arrayA; arrayA.append(QJsonValue(objA)); QJsonObject objB; objB.insert("w", "1"); objB.insert("ww", QJsonValue(arrayA)); QJsonArray arrayB; arrayB.append(QJsonValue(objB)); QJsonObject json; json.insert("w", "1"); json.insert("ww", QJsonValue(arrayB)); // 构建 Json 文档 QJsonDocument document; document.setObject(json); QByteArray byteArray = document.toJson(QJsonDocument::Compact); QString strJson(byteArray); qDebug() << strJson; QJsonParseError jsonError; QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError); if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)){ if(doucment.isObject()){ QJsonObject object = doucment.object(); if(object.contains("w")){ QJsonValue value = object.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(object.contains("ww")){ QJsonValue value = object.value("ww"); if(value.isArray()){ QJsonArray array = value.toArray(); int nSize = array.size(); for(int i = 0; i < nSize; i++) { QJsonValue value = array.at(i); if(value.isObject()){ QJsonObject object = value.toObject(); if(object.contains("w")){ QJsonValue value = object.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(object.contains("ww")){ QJsonValue value = object.value("ww"); if(value.isArray()){ QJsonArray array = value.toArray(); int nSize = array.size(); for(int i = 0; i < nSize; i++){ QJsonValue value = array.at(i); if(value.isObject()){ QJsonObject object = value.toObject(); if(object.contains("w")){ QJsonValue value = object.value("w"); if(value.isString()){ qDebug() << "w :" << value.toString() << endl; } } if(object.contains("ww")){ QJsonValue value = object.value("ww"); if(value.isString()){ qDebug() << "ww :" << value.toString() << endl; } } } } } } } } } } } }