Solved Formula in JSON
-
Hey i have a JSON like:
. . . "math": "(2.0 * value_x) + 5 * 25.0" . .
Now i want to define the value in my JSON ("value_x") in my Qt-Programm.
in Qt try this:
double value_x = 12.4; double function = value.at(0)->getValue().at(0)->math() // HERE I READ MY JSON AND I GET A STRING LIKE: "(2.0 * value_x) + 5 * 25.0" double result = HERE I WANT TO GET 2*12.4 +5 * 25.0
Any Idea how i can define a value in JSON and get the result from the formula (also from JSON) ?
-
@NotYourFan You can replace value_x with the number in the JSON string before evaluating it.
-
Hi,
Isn't that the same question as your other thread here ?
-
@jsulm
i should define the "value_x" in Qt.
"value_x" is an Input in my GUI ... so i dont know what is value_x before i defined in my GUI ....
The next Point is that the "unknow value" (in this case value_x) has every time an other name. -
@SGaist
also very similar to his other 2 topicshttps://forum.qt.io/topic/101564/mathematische-funktion-aus-einer-json-datei-lesen-und-berechnen
and
https://forum.qt.io/topic/100609/berechnungen-in-qt@NotYourFan
like I said before your best bet
is a Scriptinglanguage binding
JavaScript, Python, QML, Lua, Swift ..... -
@NotYourFan That's why I suggested to replace value_x with the number, didn't I?
See https://doc.qt.io/qt-5/qstring.html#replace-5
"has every time an other name" - then how do you know what you have to define? -
Thanks @all
i solve my problem like this:
#include <QScriptEngine> . . QScriptEngine engine; engine.globalObject().setProperty(value.at(0)->getValue.at(0).name(), value.at(0)->getValue.at(0).math()); qDebug() << "foo times two is:" << engine.evaluate("foo * 2").toNumber();
https://doc.qt.io/qt-5/qtscript-index.html
THANKS specially @J-Hilk for the bullet point "script" :)
-
You are using a bazooka to kill a fly: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
-
Thank you for your Feedback.
I do this way because my JSON-File is every time diffrent.
So with this solution my formula works fine.