Json formating output
Posted
#1
(In Topic #201)
Enthusiast

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
}
Posted
Guru

Posted
Enthusiast

Code (gambas)
- \{
- "data": \{
- "autopilot": \{
- "aircraftType": 0,
- "altHoldRate": 50
- }
- }
- }
then I run this program :
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
Posted
Guru

<IMG src="http://www.cogier.com/gambas/JSON_002.png">
</IMG>
Posted
Enthusiast

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.
Posted
Guru

Posted
Enthusiast

I just wish I knew enough to understand the two lines …. but I will keep learning
Posted
Guru

Code (gambas)
- 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
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.
<IMG src="http://www.cogier.com/gambas/Breakdown1.png">
</IMG>I hope that helps.
Posted
Enthusiast

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)
Posted
Guru

However Gambas can do this in one line with 'Join', no need for the loop above.
Code (gambas)
16k!
I started with the Sincair ZX81 (1981) which had 1k
Posted
Enthusiast

I wish there was a good book to learn all that from.
My first programming was with an HP 67 … not sure how much memory that had, but could do a lot with it.
1 guest and 0 members have just viewed this.



