Qt+json

Json文件是这样:

{
    "first fruit":
    {
        "describe":"an apple",
        "icon":"appleIcon",
        "name":"apple"
    },
    "second fruit":
    {
        "describe":"an orange",
        "icon":"orangeIcon",
        "name":"orange"
    },
    "three fruit array":
    [
        "eat 0",
        "eat 1",
        "eat 2",
        "eat 3",
        "eat 4"
    ]
}

 

存放在本地磁盘。 
利用QJson可以方便的进行解析。

   QFile loadFile("D:\\1.json");

    if(!loadFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "could't open projects json";
        return;
    }

    QByteArray allData = loadFile.readAll();//第一步读取文件到BA
    loadFile.close();

    QJsonParseError json_error;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));//第二步将BA内容转为json文档

    if(json_error.error != QJsonParseError::NoError)
    {
        qDebug() << "json error!";
        return;
    }

    QJsonObject rootObj = jsonDoc.object();//第三步获取json文档对象

    //输出所有key,这一步是非必须的,放最后的话,你可能读不到任何key
    QStringList keys = rootObj.keys();//第四步获取json文档对象的所有keys
    for(int i = 0; i < keys.size(); i++)
    {
        qDebug() << "key" << i << " is:" << keys.at(i);
    }

    //因为是预先定义好的JSON数据格式,所以这里可以这样读取
    if(rootObj.contains("first fruit"))//第五步根据keys获取内容
    {
       QJsonObject subObj = rootObj.take("first fruit").toObject();
       qDebug() << "describe is:" << subObj["describe"].toString();
       qDebug() << "icon is:" << subObj["icon"].toString();
       qDebug() << "name is:" << subObj["name"].toString();
    }

    if(rootObj.contains("second fruit"))
    {
       QJsonObject subObj = rootObj.take("second fruit").toObject();
       qDebug() << "describe is:" << subObj.take("describe").toString();
       qDebug() << "icon is:" << subObj.take("icon").toString();
       qDebug() << "name is:" << subObj.take("name").toString();
    }

    if(rootObj.contains("three fruit array"))
    {
       QJsonArray subArray = rootObj.take("three fruit array").toArray();
       for(int i = 0; i< subArray.size(); i++)
       {
           qDebug() << i<<" value is:" << subArray.at(i).toString();
       }
    }

 

需要包含的头文件:

#include <QJsonDocument>
#include <QJsonParseError>
#include <QFile>
#include <QJsonObject>
#include <QDebug>
#include <QJsonArray>

 

运行结果:

key 0  is: "first fruit"
key 1  is: "second fruit"
key 2  is: "three fruit array"
describe is: "an apple"
icon is: "appleIcon"
name is: "apple"
describe is: "an orange"
icon is: "orangeIcon"
name is: "orange"
0  value is: "eat 0"
1  value is: "eat 1"
2  value is: "eat 2"
3  value is: "eat 3"
4  value is: "eat 4"

以上转:https://blog.csdn.net/hp_cpp/article/details/80338116


PS:如何写json

QJsonObject roadJsonSubObject;
roadJsonSubObject.insert("IP", ui.lineEdit_5->text());
roadJsonSubObject.insert("PORT", ui.lineEdit_7->text());

QJsonDocument document;
document.setObject(roadJsonSubObject);
QByteArray ba = document.toJson(QJsonDocument::Compact);

 

 

 

 ps:

1、QString转QJsonObject

QString jsonStr = QString("{\"a\":\"1\", \"b\":\"2\"}");
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toLocal8Bit().data());
if( jsonDocument.isNull() ){
    qDebug()<< "===> please check the string "<< jsonStr.toLocal8Bit().data();
}
QJsonObject joson = jsonDocument.object();

2、QJsonObject转QString

QString str = QJsonDocument(priJsonObj).toJson(QJsonDocument::Compact);

 3、注意字符串和int不能随意转

{“key”:"123"}

qDebug()<<oneNodeObj["key"].toString();//"123"
qDebug()<<oneNodeObj["key"].toInt();  //0

 

 
posted @ 2018-06-12 10:16  朱小勇  阅读(426)  评论(0编辑  收藏  举报