How to exploit a json file in QT ?
-
@jsulm said in How to exploit a json file in QT ?:
@feedain Please explain better what you want to achieve.
Can you please post JSON document you want to create? So thatb we can actually understand what you are asking.Okay so, i have some numbers of port on an arduino to use by state : 0 or 1,
Example :[ { "Port": n, // n is port starts 1 to 55 "State": e, //e is 0 or 1 }, ]
It's an example but i want to create a JSON like I show you and after that to change easily the port and the state of port
I expect i explain it better... -
Ignoring the array around the object:
QJsonObject obj; obj["Port"] = p; obj["State"] = int(state);
-
QJsonObject object; pbject.insert("Port", QJsonValue(p)); object.insert("Etat", QJsonValue(e)); QJsonArray array; array.append(object); QJsonDocument doc(array); qDebug() << doc.toJson();
You should really take time to read documentation and take a look at examples like: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html
-
@Christian-Ehrlicher said in How to exploit a json file in QT ?:
Ignoring the array around the object:
QJsonObject obj; obj["Port"] = p; obj["State"] = int(state);
obj["Port"] = p; mean you create an array Port and a other State ?
-
@feedain said in How to exploit a json file in QT ?:
obj["Port"] = p; mean you create an array Port and a other State ?
?
-
@jsulm said in How to exploit a json file in QT ?:
QJsonObject object; pbject.insert("Port", QJsonValue(p)); object.insert("Etat", QJsonValue(e)); QJsonArray array; array.append(object); QJsonDocument doc(array); qDebug() << doc.toJson();
You should really take time to read documentation and take a look at examples like: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html
I look this example but there is some thing that I doesn't understand
-
@feedain said in How to exploit a json file in QT ?:
is some thing that I doesn't understand
And what exactly is the problem? How should we know it when you don't tell us what you don't understand?
-
This post is deleted!
-
@Christian-Ehrlicher said in How to exploit a json file in QT ?:
@feedain said in How to exploit a json file in QT ?:
is some thing that I doesn't understand
And what exactly is the problem? How should we know it when you don't tell us what you don't understand?
i don't understand on this example how he "create" example. For instance : i create a json file of characters of a game so we have "name" "power" "life"
How do i instanciate a new personnage with this json file ? -
@feedain said in How to exploit a json file in QT ?:
How do i instanciate a new personnage with this json file ?
Do you mean you want an array of "personnage" objects? That was in the sample code above:
QJsonArray array; array.append(object);
You can keep appending new
object
s you create, to have as many as you want?Otherwise, not sure what you mean by "a new personnage with this json file". The file contains whatever it contains, if it contains 10 "personnages" you get 10 personnages. The file does not create further objects, other than whatever is in it. You can create more objects in code, e.g. perhaps by copying some existing personnage object you have read from the file, if that is what you want to do.
-
@feedain You should really be more clear when asking.
"How do i instanciate a new personnage with this json file ?" - do you mean you want to create an instance of a class and initialise its properties from values you read from a JSON file? If so then please tell us what exactly is not clear:- How to read JSON file?
- How to initialise an instance of a class?
-
@jsulm said in How to exploit a json file in QT ?:
@feedain You should really be more clear when asking.
"How do i instanciate a new personnage with this json file ?" - do you mean you want to create an instance of a class and initialise its properties from values you read from a JSON file? If so then please tell us what exactly is not clear:- How to read JSON file?
- How to initialise an instance of a class?
I will try to be more clear. I have created my JSON template which will have two parameters: the port number and the port state.
So I have created the JSON file. But now I want to use it: let's say I want to set port 4 to state 1, how do I do that in QT? That's what I want to know, because with the debug, everything is fine, but now I want to exploit it according to the port number and the state of that port. -
- Iterate over the objects in your array using https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d
- For each object check the port whether it is the one you want to change (use https://doc.qt.io/qt-5/qjsonobject.html#operator-5b-5d)
- If you found the object, then change the State in that object (obj["State"] = int(state))
-
@jsulm said in How to exploit a json file in QT ?:
- Iterate over the objects in your array using https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d
- For each object check the port whether it is the one you want to change (use https://doc.qt.io/qt-5/qjsonobject.html#operator-5b-5d)
- If you found the object, then change the State in that object (obj["State"] = int(state))
that's my code for create the JSON file :
QJsonObject object; object.insert("port", QJsonValue(p)); object.insert("etat",QJsonValue(e)); QJsonDocument doc(object); qDebug() << doc.toJson();
I don't use array, do i must use array ?
-
@feedain said in How to exploit a json file in QT ?:
I don't use array
I'm getting tired.
Above you shown us your JSON which was composed of an array containing objects.
Could you please be so kind and explain to us how EXACTLY your JSON should look like?!
Else we all are wasting time... -
@feedain said in How to exploit a json file in QT ?:
I don't use array, do i must use array ?
If you want a whole lot of items and you don't use an array what else are you going to use? We have said several times it sounds like you want an array. It won't happen by magic.
But please respond to @jsulm post above....
-
@jsulm said in How to exploit a json file in QT ?:
@feedain said in How to exploit a json file in QT ?:
I don't use array
I'm getting tired.
Above you shown us your JSON which was composed of an array containing objects.
Could you please be so kind and explain to us how EXACTLY your JSON should look like?!
Else we all are wasting time...Yes indeed I used an array above, but I don't want to use it as it is removed in the code.
What I want to do is get an arduino port on QT and change its state without using array