Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用QJsonArray解析Qt5(C+;+;)中的JSON文件(数组)_C++_Json_Qt_Qt5 - Fatal编程技术网

C++ 如何使用QJsonArray解析Qt5(C+;+;)中的JSON文件(数组)

C++ 如何使用QJsonArray解析Qt5(C+;+;)中的JSON文件(数组),c++,json,qt,qt5,C++,Json,Qt,Qt5,我在试Qt5。我尝试从json文件中读取值。Json文件与上述文件类似: test.json [{"w":188,"h":334,"th":0.350000,"l":232,"r":420,"t":133,"b":467,"p":0.713963,"n":"person"}] [{"w":127,"h":141,"th":0.350000,"l":1152,"r":1279,"t":162,"b":303,"p":0.408129,"n":"person"},{"w":179,"h":339,"

我在试Qt5。我尝试从json文件中读取值。Json文件与上述文件类似:

test.json

[{"w":188,"h":334,"th":0.350000,"l":232,"r":420,"t":133,"b":467,"p":0.713963,"n":"person"}]
[{"w":127,"h":141,"th":0.350000,"l":1152,"r":1279,"t":162,"b":303,"p":0.408129,"n":"person"},{"w":179,"h":339,"th":0.350000,"l":230,"r":409,"t":131,"b":470,"p":0.698172,"n":"person"}]
我试着用密码。如何读取这样的json文件结构

QString val;
QFile file;
file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
//file is readall
val = file.readAll();
file.close();
qWarning() << val; //print consol
QJsonDocument jsonDocument = QJsonDocument::fromJson(val.toUtf8());
//get data array !!!
QJsonObject jsonObject = jsonDocument.object();
QJsonArray jsonArray = jsonObject["w"].toArray();
qWarning() << jsonArray[0].toString();
QString val;
QFile文件;
setFileName(“test.json”);
file.open(QIODevice::ReadOnly | QIODevice::Text);
//文件为readall
val=file.readAll();
file.close();

qWarning()由于数据没有JSON格式(格式错误,请参阅),但如果数据是部分的,我们必须做的是将其分开,因为我们使用
qregularexpression
,并验证数据是否具有适当的格式,那么代码与您的代码类似

代码:

#include <QCoreApplication>
#include <QFile>
#include <QDebug>
#include <QRegularExpression>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>

int main(int argc, char *argv[]){

    QCoreApplication a(argc, argv);

    QFile file("test.json");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qWarning() << "Could not open file!";
        return 0;
    }
    const auto& data = QString(file.readAll());
    file.close();

    QRegularExpression regex("\\[|\\]");
    const auto& jsons = data.split(regex);

    for(const auto& json : jsons)
        if(!json.trimmed().isEmpty()){
            const auto& formattedJson = QString("[%1]").arg(json);
            const auto& doc = QJsonDocument::fromJson(formattedJson.toUtf8());

            if(doc.isArray())
                for(const auto& item : doc.array()){
                    const auto& obj = item.toObject();
                    const auto& keys = obj.keys();

                    for(const auto& key : keys){
                        if(key == "n")
                            qDebug() << key << obj[key].toString();
                        else
                            qDebug() << key << obj[key].toInt();
                    }
                }
        }

    return a.exec();
}
"b" 467
"h" 334
"l" 232
"n" "person"
"p" 0
"r" 420
"t" 133
"th" 0
"w" 188
"b" 303
"h" 141
"l" 1152
"n" "person"
"p" 0
"r" 1279
"t" 162
"th" 0
"w" 127
"b" 470
"h" 339
"l" 230
"n" "person"
"p" 0
"r" 409
"t" 131
"th" 0
"w" 179

真正的问题是什么?有什么不符合您的预期以及原因?此文本的可能副本不符合json格式<代码>[values1][values2]
非json您可以在以下链接中验证它是否为适当的json格式:OK。我的json文件格式,每行[blank]或[object{1}]或[{1}对象,对象{2}….]都可以形成。我正在尝试阅读这种格式。但是我不能正确地阅读这种格式的Qt5。如何以这种格式阅读。为什么不在
'\n'
处拆分
而不是使用正则表达式?@Azeem我假设最一般的情况,我也想过应用相同的东西,但数据可能在一行中,我的解决方案涵盖了这两种情况。@Azeem考虑最坏的情况总是好的,我唯一能确定的是,日期将介于
[]
@eyllanesec之间:我明白你的意思,对于给定的格式错误的JSON格式,它似乎没问题。谢谢你的回答。我想它会起作用的@埃利亚内塞克