Closed liufeijin closed 2 years ago
Hi! You can do this trick:
QJSValue Convert(QJSEngine* engine, const QJsonValue& val)
{
if (val.isBool())
{
return QJSValue(val.toBool());
}
else if (val.isString())
{
return QJSValue(val.toString());
}
else if (val.isDouble())
{
return QJSValue(val.toDouble());
}
else if (val.isNull())
{
return QJSValue(QJSValue::NullValue);
}
else if (val.isUndefined())
{
return QJSValue(QJSValue::UndefinedValue);
}
else if (val.isObject())
{
QJsonObject obj = val.toObject();
QJSValue newobj = engine->newObject();
for (auto itor = obj.begin(); itor != obj.end(); itor++)
{
QString key = itor.key();
QJsonValue value = itor.value();
QJSValue convertedValue = Convert(engine, value);
newobj.setProperty(key, convertedValue);
}
return newobj;
}
else if (val.isArray())
{
QJsonArray arr = val.toArray();
QJSValue newobj = engine->newArray(arr.size());
for (int i = 0; i < arr.size(); i++)
{
QJsonValue value = arr[i];
QJSValue convertedValue = Convert(engine, value);
newobj.setProperty(i, convertedValue);
}
return newobj;
}
// ASSERT(FALSE && "This shouldn't happen");
return QJSValue(QJSValue::UndefinedValue);
}
// https://stackoverflow.com/questions/49658551/convert-qjsonobject-to-javascript-object
.......
QJsonObject jsonItemObj;
jsonItemObj["Street"] = "Downing Street 10";
jsonItemObj["City"] = "London";
jsonItemObj["Country"] = "Great Britain";
report->scriptManager()->scriptEngine()->globalObject().setProperty(
"test", Convert(report->scriptManager()->scriptEngine(),jsonItemObj)
);
in the report: $S{test["Street"]}
Hi fralx Many thanks!
Hi! You can also use a different approach :)
report->dataManager()->setReportVariable("Json", jDoc.toJson()); .... $S{ let j = JSON.parse(getVariable("Json")) j["City"] }
Hi! You can also use a different approach :)
report->dataManager()->setReportVariable("Json", jDoc.toJson()); .... $S{ let j = JSON.parse(getVariable("Json")) j["City"] }
This is better.
Dear Fralx Do you have plan for adding QJson type variable in LimeReport? When using Limereport, need pass some datas to Limereprot variable from my QT app, if it is QJson type will need a lot of code to split QJson to QString and pass to Limereport. If Limereport can support QJson variables , just pass a few data to Limereprot and there is only a few variables in Limereport also. It will be concise.