Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++ 用Qt解析JSON_C++_Mysql_Json_Qt_Parsing - Fatal编程技术网

C++ 用Qt解析JSON

C++ 用Qt解析JSON,c++,mysql,json,qt,parsing,C++,Mysql,Json,Qt,Parsing,嗨,我有一个JSON的JSON: (一行中没有全部返回) 我将解析它并将其放入MYSQL表中。 并非全部,首先是姓名和MMSI。 但我不想在我的脑海里看到任何东西,因为它不会跳到前面: bool ok = true; // my json data is in reply & ok is a boolean QVariantList result = parser.parse(reply, &ok).toList(); foreach(QVariant record, res

嗨,我有一个JSON的
JSON
: (一行中没有全部返回)

我将解析它并将其放入
MYSQL
表中。 并非全部,首先是姓名和
MMSI
。 但我不想在我的脑海里看到任何东西,因为它不会跳到前面:

bool ok = true;

// my json data is in reply & ok is a boolean
QVariantList result = parser.parse(reply, &ok).toList();

foreach(QVariant record, result) {
    QVariantMap map = record.toMap();
    qDebug() << map.value("NAME");
}
bool ok=true;
//我的json数据在回复中&ok是一个布尔值
qvariantlistresult=parser.parse(reply,&ok).toList();
foreach(QVariant记录、结果){
QVariantMap=record.toMap();

qDebug()您的代码看起来像是在顶级数组上迭代,而您要查找的数据位于嵌套数组中,嵌套数组实际上是顶级数组的第二项。因此,您需要在内部数组中的项上迭代

以下代码适用于您的示例JSON:

QVariantList result = parser.parse(reply, &ok).toList().at(1).toList();
foreach (const QVariant &item, result) {
    QVariantMap map = item.toMap();
    qDebug() << map["NAME"].toString();
    qDebug() << map["MMSI"].toLongLong();
}
qvariantlistresult=parser.parse(reply,&ok).toList().at(1).toList();
foreach(常量变量和项目、结果){
QVariantMap=item.toMap();

qDebug()如果您使用的是Qt5或更高版本,则可以使用和提供的强大功能

我已将您的json复制到我的D-drive中名为test.txt的文件中,下面的代码运行良好

QJsonDocument jsonDoc;
QByteArray temp;
QFile file("D://test.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
    temp = file.readAll();
}
jsonDoc = QJsonDocument::fromJson(temp);
QJsonArray jsonArray = jsonDoc.array().at(1).toArray(); //Since you are interested in the json array which is the second item and not the first json object with error
for(int i =0; i < jsonArray.size(); ++i)
{
    QJsonObject jsonObj = jsonArray.at(i).toObject();
    int mmsi = jsonObj.find("MMSI").value().toInt();
    QString name = jsonObj.find("NAME").value().toString();
    qDebug() << mmsi;
    qDebug() << name;
}
QJsonDocument-jsonDoc;
QByteArray温度;
QFile文件(“D://test.txt”);
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
temp=file.readAll();
}
jsonDoc=QJsonDocument::fromJson(temp);
QJsonArray jsonArray=jsonDoc.array().at(1).toArray();//因为您对json数组感兴趣,它是第二个项,而不是第一个json对象,有错误
对于(int i=0;iqDebug()是的,但我必须使用Qt4。那么我猜您已经在使用QJson了。调用
parse()后,
ok
true
?@thelittlePanda,我已经用我对这个问题的猜测更新了我的答案。我一直在使用库:QJson::Parser Parser;没人能告诉我我的代码出了什么问题吗?Json没问题。我使用QJson库。但我的问题是它没有跳转到foreach中。是的,我已经尝试过了,但当我使用它时,它会出现这个错误:ASSQList中的ERT失败::at:“索引超出范围”,文件/usr/include/QtCore/QList.h,第469行,刚刚看到您需要Qt4中的解决方案。我建议您按照我在回答中的建议进行尝试。我也给出了一个代码段。您是否找到解决此问题的替代方案?如果是,请与其他人共享。
QJsonDocument jsonDoc;
QByteArray temp;
QFile file("D://test.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
    temp = file.readAll();
}
jsonDoc = QJsonDocument::fromJson(temp);
QJsonArray jsonArray = jsonDoc.array().at(1).toArray(); //Since you are interested in the json array which is the second item and not the first json object with error
for(int i =0; i < jsonArray.size(); ++i)
{
    QJsonObject jsonObj = jsonArray.at(i).toObject();
    int mmsi = jsonObj.find("MMSI").value().toInt();
    QString name = jsonObj.find("NAME").value().toString();
    qDebug() << mmsi;
    qDebug() << name;
}