how do I output a file using Json with line feeds and tabs ?
this is what I get now:
{"data":{"autopilot":{"aircraftType":0,"altHoldRate":10,"attitude":{"0":{"0":{"envelopeNeg":0,"envelopePos":0,"feature":128},"1":{"envelopeNeg":0,"envelopePos":0,"feature":128}},"1":{"0":{"envelopeNeg":0,"envelopePos":0,"feature":128},"1"
what I would like is this:
{
"data": {
"autopilot": {
"aircraftType": 0,
"altHoldRate": 50,
"attitude": {
"0": {
"0": {
"envelopeNeg": 0,
"envelopePos": 0,
"feature": 128
},
"1": {
"envelopeNeg": 0,
"envelopePos": 0,
"feature": 128
}
Json formating output
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Json formating output
Can you upload the JSON file please.
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: Json formating output
this is my original file :
{"data":{"autopilot":{"aircraftType":0,"altHoldRate":50}}}
How can I format JSON to get the first format?
So either the File.Load or the File.save have remove linefeeds and spaces, or is it the use of a Collection variable
{
"data": {
"autopilot": {
"aircraftType": 0,
"altHoldRate": 50
}
}
}
then I run this program :Public Sub Form_Open()
Dim cReciverDta As Collection
cReciverDta = JSON.Decode(File.Load("~/airplane files/testfile.txt"))
File.save(("~/airplane files/testfile2.txt"), JSON.Encode(cReciverDta))
End
And the optput I get is this :{"data":{"autopilot":{"aircraftType":0,"altHoldRate":50}}}
How can I format JSON to get the first format?
So either the File.Load or the File.save have remove linefeeds and spaces, or is it the use of a Collection variable
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Json formating output
Have a look at the attached program. Hopefully this will help.
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: Json formating output
Thank you for your reply
All I get from the download is the same thing you have in your post and a list of clients.
I must be doing something wrong with the download.
I think the use of the Collection variable is what is removing the format from the data.
so I will have to use a string or string array to modify the data before I write it back to the file.
If json had Prety Print, I think that would solve the problem.
All I get from the download is the same thing you have in your post and a list of clients.
I must be doing something wrong with the download.
I think the use of the Collection variable is what is removing the format from the data.
so I will have to use a string or string array to modify the data before I write it back to the file.
If json had Prety Print, I think that would solve the problem.
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Json formating output
Try changing the line in my program from: -
Dim sFile As String[] = Split(File.Load("../json.txt"), gb.NewLine)
To: -Dim sFile As String[] = Split(File.Load("~/airplane files/testfile.txt"), gb.NewLine)
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: Json formating output
Thanks that works
I just wish I knew enough to understand the two lines .... but I will keep learning
I just wish I knew enough to understand the two lines .... but I will keep learning
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Json formating output
Let's try and breakdown the line Dim sFile As String[] = Split(File.Load("~/airplane files/testfile.txt"), gb.NewLine) for you by separating out the individual commands. The attached program does the same as the single line. I have added a Variable along with a Print and Stop command.
I hope that helps.
Public Sub Form_Open()
Dim sFile As String[] 'To store an array
Dim sLoad As String 'To store a string
sLoad = File.Load("~/airplane files/testfile.txt") 'Load the file, ~/airplane files/testfile.txt, into the string sLoad
Print sLoad 'Print the file, just so that you can see what's happening
sFile = Split(sLoad, gb.NewLine) 'Take the file(sLoad) and Split each line and put it in the array sFile
Stop 'Stop the program
End
If you run the attached program it will stop at "Stop". When it does use the mouse to highlight sFile and you will see what is in the sFile array.I hope that helps.
- grayghost4
- Posts: 187
- Joined: Wednesday 5th December 2018 5:00am
- Location: Marengo, Illinois usa
Re: Json formating output
Thank you very much, that is a big help.
My last programming was about 40 years ago on an Apple II with integer basic then some UCSD Pascal.
Now I want to get back to programming again, and Gambas looks like it is the best one for me.
Sure is nice to have more than 16 K memory and a Tape drive to save to.
One more question, after I make changes to the array, what is the easiest way to convert it back to a file?
I came up with this .....
For iCount = 0 to sFile.count -1
sLoad2 &= sFile[iCount] & gb.NewLine
Next
File.save(("~/testfile2.txt"), sLoad2)
My last programming was about 40 years ago on an Apple II with integer basic then some UCSD Pascal.
Now I want to get back to programming again, and Gambas looks like it is the best one for me.
Sure is nice to have more than 16 K memory and a Tape drive to save to.
One more question, after I make changes to the array, what is the easiest way to convert it back to a file?
I came up with this .....
For iCount = 0 to sFile.count -1
sLoad2 &= sFile[iCount] & gb.NewLine
Next
File.save(("~/testfile2.txt"), sLoad2)
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Json formating output
Your code works but you can use .Max instead of .Count -1 and you don't need so many brackets in the 'Save' line.
I started with the Sincair ZX81 (1981) which had 1k
For iCount = 0 To sFile.Max
sLoad2 &= sFile[iCount] & gb.NewLine
Next
File.save("~/testfile2.txt", sLoad2)
However Gambas can do this in one line with 'Join', no need for the loop above.File.save("~/testfile2.txt", sFile.Join(gb.newline))
16k! What luxury!!I started with the Sincair ZX81 (1981) which had 1k