This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Using GSON to deserialize a JSON String  RSS feed

 
Ranch Hand
Posts: 103
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man I thought it'd be easy to use GSON, apparently it isn't as easy to deserialize a JSON String as it is to serialize...

Let me guide you through the process; i'm making a dieting app for a friend of mine and I'm using JSON to store the diets, later I'll use them to create HTML reports and such. In my GUI's controller, I have a DietSchedule object that gets initialized as such:


From what I've read online, the problem is that the types collections hold, do not get "saved" or something along these lines, I don't have much experience with generics and (de)serialization. I believe there is a solution to this (from what I've read) but I'm too much of a noob to understand it or know where to fix it in my code.

In my controller I'm using ObservableLists (JavaFX8), so I thought it would be better to use List in the MealLists since it's a supertype and thus would accept every sub-type.

I'm attaching the basics of my classes, any help would be greatly appreciated (I don't want to save to XML instead of JSON, XML is more difficult to handle than JSON in HTML [according to a friend of mine that is]).



 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vasilis Souvatzis wrote:Man I thought it'd be easy to use GSON...


There's a LOT of stuff in that post, which leads me to suspect that you're flapping around for a solution.

I'm no GSON or JSON expert, but the first thing I would do is to break the problem down.

And the first thing is, does:
DietSchedule schedule = gson.fromJson(jsonContents, DietSchedule.class);
return you anything?

If not, THAT'S what you need to tackle. Forget what you want to do with it, just make sure that you can get ONE DietSchedule object as you want it. EVERY TIME. Once you have that, everything else is Java.

HIH

Winston
 
Author and ninkuma
Marshal
Posts: 66810
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List are tricky. I've had luck with:
 
Vasilis Souvatzis
Ranch Hand
Posts: 103
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*sigh* I fixed it... It had nothing to do with JSON after all...

Bear I appreciate your code snippet and I'll keep it for future reference, but it was Winston that indirectly gave me the solution

In my MealLists class I had List sth when it should have been List<Food> sth!!! No surprise it didn't work, I was using Generics when I shouldn't since I know exactly what class objects my lists hold.

So after I changed that code bit, everything works like a charm It wasn't GSON, it wasn't JSON, it was pure Java and it was my bad...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!