• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

JSON simple write problem

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object with a string and multiple arrays that I am trying to write to a json file.

Here is the object I am trying to write:

Here is the code I am using to write the first two elements with:

Finally, here is the expected and actual result:

Can someone help me understand what is wrong?
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the expected results in the op above, "Auto" should be "Manual"
 
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don;t know if this is the best way with that library, but this is a way:


PS: method names and field names should use lower camel case: scheduleToFile(), control, manualZones, onTime, etc.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prints:
{"Control":"Manual","OnTime":[12,59,0],"ManualZones":[true,false,true,true,true,true,true],"Zones":[1,2,3,4,5,6,7]}
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Prints:
{"Control":"Manual","OnTime":[12,59,0],"ManualZones":[true,false,true,true,true,true,true],"Zones":[1,2,3,4,5,6,7]}



Yes, that works quite well. Thanks.

I noticed the output isn't in the same order as how things were readied for output. It doesn't seem to matter, but it did catch my attention.
 
Ron McLeod
Saloon Keeper
Posts: 2722
354
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gauthier wrote:I noticed the output isn't in the same order as how things were readied for output. It doesn't seem to matter, but it did catch my attention.


The storage for the key/value pairs in the the JSONObject is probably implemented using a Map which doesn't guarantee order.  It shouldn't matter because the object pairs in a JSON representation aren't expected to be ordered.  Arrays are different though; the values are expected to be represented in order.

From json.org:
     - An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
     - An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
 
It will give me the powers of the gods. Not bad for a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!