This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Highscores using JSON and TableView  RSS feed

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

So I have re-created the PacMan game using JavaFX. In this game, I save a highscore automatically after death using JSON. This creates a file named "json.data.txt" where the name of the player, score and date is registered and saved.
I would now like to 'load/read' this JSON file into a pane with a TableView with 4 columns: place, name, score and date.

I would like to know how I could do this. As I am quite aware that it isn't really something easy, as the data has to be read from the file and then has to be in the correct column.
Please help me out, thank you in advance =).

PS: If you wish to see some code, feel free to let me know.

-Ax
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should add that I already have this code:

 
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with this?  Do you get errors?  If so, post the complete error message.  Is it not doing what you expect?  If so, what do you expect?
 
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you don't save the data in a .json file, you save it in a textfile
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I worked out a quick example:





The fxmlfile is a basic tableview with two comlumns
highscores.png
[Thumbnail for highscores.png]
Textfile used
result.png
[Thumbnail for result.png]
The end result
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I worked out a quick example:

The fxmlfile is a basic tableview with two comlumns


If I use that code for my class, the tableview gets messed up and is unreadable.

This is my HiScore class:


And an autosave for my hiscores, in my controller (not sure if this will work):


Not sure what causes my tableview to get all messed up.
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Txt looks like this:

Staff note (Knute Snortum):

Two things to remember: 1) Be sure that the URL between the img tags points to the actual image and not an HTML page with an image. Use the Preview button next to the Submit button to be sure. 2) Don't use images to post text. Copy and paste.

 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why are you trying to read json from a textfile?
How is your file structured?
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:but why are you trying to read json from a textfile?
How is your file structured?


The file is just a .txt, my teacher thought this would be more convenient. Also, my text file looks like this:



So first username, then the score and then the date.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're better to read the objects as a whole then to split the textfile like I was doing.
here is how to do that...
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:you're better to read the objects as a whole then to split the textfile like I was doing.
here is how to do that...



I tried it like this:


but it gives an error saying: "JsonElement cannot be converted to String" ?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I'm home I'll try to recreate it using your data
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:When I'm home I'll try to recreate it using your data


Thanks. The JSON/GSON is quite hard for me to understand.. It's so stupid that we have to use it, I heard mySQL is better to save data.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is, but it would take a database of course
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:It is, but it would take a database of course


Yeah! But we have to use JSON.. So I'll have to stick with it..
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way... Why are you using a timestamp and not a LocalDate?
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:By the way... Why are you using a timestamp and not a LocalDate?


Not sure, what's the difference?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
timestamp is more used with db's and is part of java.util.Date, which is kinda fallen in unuse cause it's not threadsafe or inmutable
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:timestamp is more used with db's and is part of java.util.Date, which is kinda fallen in unuse cause it's not threadsafe or inmutable


Oh good to know, I'll adjust that. Is it similar? E.g. getLocalDate(); ?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in the same timezone, so it will most likely be around 8thish when I post a update version of your program, I'll also try to optimize your code a bit, for examply you shouldn't keep the code to read and write in your objectClass
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Where in the same timezone, so it will most likely be around 8thish when I post a update version of your program, I'll also try to optimize your code a bit, for examply you shouldn't keep the code to read and write in your objectClass


All right! Thanks in advance! I'll have a look at this topic around 8pm!
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the documentation on LocalDate.
For what you're trying to do you just need LocalDate.now(), which will give you the current date based on your systemsettings
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:here is the documentation on LocalDate.
For what you're trying to do you just need LocalDate.now(), which will give you the current date based on your systemsettings


Perfect! I'll already implement this. Thanks.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know what your problem is, but now just finding a fix
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I know what your problem is, but now just finding a fix


Take your time, is it hard?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has to do with the date in your json object, it needs to be deserialized, I tried to do that, but failed, so I created a post for some assistance.
These things should be mentioned by your instructor, have yoy worked with json before in that course?
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:It has to do with the date in your json object, it needs to be deserialized, I tried to do that, but failed, so I created a post for some assistance.
These things should be mentioned by your instructor, have yoy worked with json before in that course?


This is the first time I encounter JSON, we never had any examples in class about it whatsoever.
I'm a structural/industrial engineering student and Graphical Applications in JavaFX is a subject we have, hence I don't know much about it as it as rather a side-subject.
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify: it was your decision to use JSON, not the instructor's?  If that's the case, I wouldn't use JSON to store your object information on the disk; I'd use Java Serialization or just plain old CSV (comma separated values) files.
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...or maybe a very simple DB (database).
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Just to clarify: it was your decision to use JSON, not the instructor's?  If that's the case, I wouldn't use JSON to store your object information on the disk; I'd use Java Serialization or just plain old CSV (comma separated values) files.


The instructor told us that we have to use JSON, so it wasn't my decision. I know there is plenty of other ways to do the data storing, but we really have to use this one..
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.  I was confused by what you wrote:

This is the first time I encounter JSON, we never had any examples in class about it whatsoever.


So the instructor wants you to use JSON without ever talking about it in the class?
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A day ago, Axel Rey wrote:

Daniel Demesmaecker wrote:but why are you trying to read json from a textfile?
How is your file structured?


The file is just a .txt, my teacher thought this would be more convenient.


I don't like bashing teachers, but in this one instance, your teacher was wrong.  Parsing JSON is no trivial matter.  You started with using Gson from Google.  Did you abandon this?  (I need to re-read this whole thread.)
 
Knute Snortum
Sheriff
Posts: 5742
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.  Axel, try writing a class that just posts and reads JSON from the disk using Gson.  Daniel has already posted a way to get the data into a JavaFX Table.
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to figure it out.
This is a working example (note: If you want the dateString to include a time too, you'll need to use LocalDateTime, instead of LocalDate)
Textfile with jsonObject:

PlayerClass

MenuController:

AddControler:

ShowController:
result.png
[Thumbnail for result.png]
tableview
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I managed to figure it out.
This is a working example (note: If you want the dateString to include a time too, you'll need to use LocalDateTime, instead of LocalDate)
Textfile with jsonObject:

PlayerClass

MenuController:

AddControler:

ShowController:



I will try this, which JSON jar are you using?
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gson 2.2.2
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:gson 2.2.2



Thanks, I'll let you know the outcome.
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't use obj.put, it says: "Cannot find symbol. Variable obj of type JsonObject."
 
Daniel Demesmaecker
Master Rancher
Posts: 1161
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oeps forgot to mention, you need to put json.simple on your classpath
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:oeps forgot to mention, you need to put json.simple on your classpath


Where/how do I do this? I never worked with JSON simple.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!