• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to read JSON files using Gson

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to read a Jason file but nothing happens when I press the read button there is not even an exception.
I'm using Gson and my Jason reader is


and the code for my user interface is


The datalist class is an arraylist which I use for all the files instead of having separate arraylists. Can anybody see the problem in my code please?
Thanks a lot
 
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saeid

Here a simple example to test the JSON read and write. (Also reference where I got the info)
Just to change the name of destination file in the 2 class below.
Hope it will help.






Regards

Christian
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer to use Gson and I'm sure my code is almost done and I'm just missing something small. I have three file readers which two of them are done I'm just stuck at reading Jason
 
Marshal
Posts: 4664
583
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can post you an example of what the contents of a file with JSON-encoded sensor data looks like?
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Can post you an example of what the contents of a file with JSON-encoded sensor data looks like?



 
Ron McLeod
Marshal
Posts: 4664
583
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that  in the JSON data, the list of sensor data is named logdata, but there is no field named logdata in your Datalist class (by default, when deserializing, Gson will try and match the JSON attribute name with a Java field name).

Use the SerializedName annotation to indicate that the annotated field is for the JSON data named logdata.

For example (using your code from another topic):
 
Ron McLeod
Marshal
Posts: 4664
583
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, there is no need to create an instance of Datalist in your JASONReader class (unless it is used for something else).
 
Ron McLeod
Marshal
Posts: 4664
583
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you can simplify your reader.
 
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saeid jamali wrote:and the code for my user interface is


Don't use ==, use equals. Otherwise the block will not get executed if your description is equal to JSON but is a different String instance.
 
Rob Spoor
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And two more things:
1) It's JSON, not JASON (so also not jason).
2) Make sure to close your streams. Using Ron's simplified code:
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic