How to insert entry to json file
-
Thanks for clarifying regarding the order.
Yes I now realize that I created QJsonDocument using commandsArray that I modified and it does not contain the main JsonObject "commands" . I have try to check if the change I made to commandsArray affect jsonresponse but unfortunately it does not.
I looked up how to add jsonarray inside another json object here (for java language):
https://stackoverflow.com/questions/12142238/add-jsonarray-to-jsonobjectand they use put() method, so something like:
QJsonObject jsonobject; jsonobject.put("commands", commandsArray);
Unfortunately, QT does not have this method and I was not able to find a relevant method for adding a QJsonArray inside another QJsonObject.
I looked up how to add jsonarray inside another json object here (for java language):
I am lost. You are not trying to add a new array inside anything, you are trying to add a new element to an existing array. As for looking at Java, that won't get you anywhere :)
I have try to check if the change I made to commandsArray affect jsonresponse but unfortunately it does not.
I don't think this is correct, but I do not have time right now to check. I suggest you try a tiny example to verify this.
Works without any issues. The file now contains everything:
OK, so you are good now anyway!
-
@ChrisW67 Ahh thanks for clarifying regarding the fileseek.
Also, the file size (in lines) can be much bigger than 100. This is just an example
@lukutis222 said in How to insert entry to json file:
Also, the file size (in lines) can be much bigger than 100. This is just an example
That may be so, but your code created an array of 100 QStrings to load the lines into:
int line_count=0; QString line[100]; ... while( !in.atEnd()) { line[line_count]=in.readLine(); line_count++; }
It does not seem to me that this will handle a file with 101 lines.