• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parse JSON in Java without knowing the incoming JSON format  RSS feed

 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to Parse JSON in Java without knowing the incoming JSON format?

For example i have the following incoming input:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which JSON framework?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of different open source Java libraries for working with JSON. One that's popular and that I like is Jackson. It has a JSON binding API, which means that it can automatically translate JSON from and to Java objects (Java beans), but it also has an API to work with JSON when you don't know the exact content, and you don't have Java beans to map the JSON from or to.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

any framework will do, but preferably google GSON if it can do this.
do you guys mind to give some example? so that i know where i can start from.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you have that in the wrong order. Once you have chosen a framework then it would be possible to produce an example for that framework -- and I think most of the frameworks you should be looking at already have websites with introductory examples. So choose a framework first.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use Google Gson, then have a look at the Google Gson User Guide - it has lots of examples of how to use Gson.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys, Thanks for the guidance. I think it's better for me to describe my main problem here:

I need to call this API, but it's a painful process to keep constructing big and large structure of Java Object with multiple nested Class in order to parse the JSON response to JAVA Object. Example JSON Response


I have really no idea on whats the best way to Parse those incoming big JSON response to JAVA Class. Kindly Advise
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like Jackson, Gson has an API to parse JSON into generic JsonObject, JsonArray etc.

Look at the API documentation.

A simple example:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A great deal depends what you are going to do with that data after parsing. Do you need to reference it and use it throughout the remainder of the application? Or are you you just going to dump it somewhere and the specifics aren't important?

I suspect the former, so I don't understand why it's such a "burden" to create classes that represent the data. That's what Java classes are for. Once in the class instances, the data is easy to deal with throughout the rest of the application.

Trying to "get around" classes that represent the data is just going to introduce complications that will likely infect the rest of the application. So I'm going to ask the same question that you never really answered in your other post on JSON: what do you have against creating classes that model the data?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear says, how is this data used?

What is your model (forget the incoming data)...what do you need, and what do you need it to look like?
You're getting this stuff for a reason, I presume.

Once you know that then you have a model to map to.
And you can use the stuff Jesper mentions.

If you need it all, though, then you might want to think about just modelling the incoming data and letting the framework do the work.
But without knowing what it is this data is actually for, all we can do is throw pretty random stuff out there.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Basically, i will still use those returned data throughout the application, i need to pass those data to another internal API for some analytics purposes. At the end, i will still need to utilise all those retrieved info. Seems like i cannot run away from constructing a Java Class still.
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

you know what, after few days of struggling, it seems like still Creating Java Class to represent those JSON is the best for now. After listening to all the advice from you guys, i feel more confident on my way of Constructing Java with nested Class then.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeansonne Pierre wrote:you know what, after few days of struggling, it seems like still Creating Java Class to represent those JSON is the best for now. After listening to all the advice from you guys, i feel more confident on my way of Constructing Java with nested Class then.


Of course that's the best approach. Java classes are meant to model data. Trying to avoid that is still a mystery to me.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!