• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to read JSON files using Gson  RSS feed

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
 
Christian Klugesherz
Ranch Hand
Posts: 103
3
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ron McLeod
Saloon Keeper
Posts: 1432
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 1432
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 1432
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 1432
207
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you can simplify your reader.
 
Rob Spoor
Sheriff
Posts: 20897
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 20897
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!