[SOLVED]parse Json in Qt5
-
I want parse a json and add it into sqlite, this json file parse from php
test.json
[{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]
qt code
QFile file; file.setFileName("test1.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); val = file.readAll(); file.close(); QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8()); //get the jsonObject QJsonObject jObject = doc.object(); //convert the json object to variantmap QVariantMap mainMap = jObject.toVariantMap(); //convert the json object to variantmap QVariantMap dataMap = mainMap['0'].toMap(); qDebug() << dataMap['name'].toString();
but an error produced : invalid user-defined conversion from 'char' to 'const QString&' [-fpermissive]
QVariantMap dataMap = mainMap['1'].toMap();
^ -
@yeckel thank you
updated code block
QString val; QFile file; file.setFileName("test1.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); val = file.readAll(); file.close(); QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8()); //get the jsonObject QJsonObject jObject = doc.object(); //convert the json object to variantmap QVariantMap mainMap = jObject.toVariantMap(); //convert the json object to variantmap QVariantMap dataMap = mainMap[1].toMap(); qDebug() << dataMap["name"].toString();
I tried that too but still got error
error: invalid conversion from 'int' to 'const char*' [-fpermissive]
-
@4j1th Aa, sorry I thought you have the array already. mainMap is type of QVariantMap thus it needs QString as a key parameter.
Watch out your json example is not a json. Something like: {"results": [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]} woud be.
Then:
QVariantMap mainMap = jObject.toVariantMap();
QVariantList localList = mainMap["result"].toList();
QVAriantMap map = localList[0].toMap();
qDebug() << map["name"].toString; -
@yeckel
json file
{"results":[{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]}
qt code
//get the jsonObject QJsonObject jObject = doc.object(); QVariantMap mainMap = jObject.toVariantMap(); QVariantList localList = mainMap["result"].toList(); QVariantMap map = localList[1].toMap(); qDebug() << map["name"].toString();
compiled but ASSERT failure in QList<T>::operator[]: "index out of range", file ../../../../Qt5.4.1/5.4/gcc_64/include/QtCore/qlist.h, line 486
The program has unexpectedly finished. -
@4j1th
QJsonParseError jsonError;
QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError);
if (jsonError.error != QJsonParseError::NoError){
qDebug() << jsonError.errorString();
}
QList<QVariant> list = flowerJson.toVariant().toList();
QMap<QString, QVariant> map = list[0].toMap();
qDebug() << map["name"].toString();Has to work.
-
@yeckel Thank you very much it's working now
json file
[{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]
Qt code
QFile file; file.setFileName("test1.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); QJsonParseError jsonError; QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError); if (jsonError.error != QJsonParseError::NoError){ qDebug() << jsonError.errorString(); } QList<QVariant> list = flowerJson.toVariant().toList(); QMap<QString, QVariant> map = list[0].toMap(); qDebug() << map["name"].toString();