Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JSON objects directly from model classes in Java

Tags:

java

json

sockets

I have some model classes like Customer, Product, etc. in my project which have several fields and their setter-getter methods, I need to exchange objects of these classes as a JSONObject via Sockets to and from client and server.

Is there any way I can create JSONObject directly from the object of model class such that fields of the object become keys and values of that model class object become values for this JSONObject.

Example:

Customer c = new Customer(); c.setName("Foo Bar"); c.setCity("Atlantis"); ..... /* More such setters and corresponding getters when I need the values */ ..... 

And I create JSON Object as:

JSONObject jsonc = new JSONObject(c); //I'll use this only once I'm done setting all values. 

Which gets me something like:

{"name":"Foo Bar","city":"Atlantis"...} 

Please note that, in some of my model classes, certain properties are itself an object of other model class. Such as:

Product p = new Product(); p.setName("FooBar Cookies"); p.setProductType("Food"); c.setBoughtProduct(p); 

In a case like above, as I'd expect, the yielded JSON object would be:

{"name":"Foo Bar","city":"Atlantis","bought":{"productname":"FooBar Cookies","producttype":"food"}} 

I know I could create something like toJSONString() in each model class and have the JSON-friendly string created and manipulate it then, but in my previous experiences of creating RESTful service in Java (which is totally out of context for this question), I could return JSON string from the service method by using @Produces(MediaType.APPLICATION_JSON) and have the method returning object of model class. So it produced JSON string, which I could consume at the client end.

I was wondering if it's possible to get similar behavior in current scenario.

like image 497
Kushal Avatar asked Jun 07 '12 11:06

Kushal


People also ask

Can we create JSON object Java?

Underscore-java can create json from an object.

How do I create multiple JSON objects?

JSONArray pdoInformation = new JSONArray(); JSONObject pDetail1 = new JSONObject(); JSONObject pDetail2 = new JSONObject(); JSONObject pDetail3 = new JSONObject(); pDetail1. put("productid", 1); pDetail1. put("qty", 3); pDetail1. put("listprice", 9500); pDetail2.


2 Answers

Google GSON does this; I've used it on several projects and it's simple and works well. It can do the translation for simple objects with no intervention, but there's a mechanism for customizing the translation (in both directions,) as well.

Gson g = ...; String jsonString = g.toJson(new Customer()); 
like image 138
Ernest Friedman-Hill Avatar answered Oct 10 '22 18:10

Ernest Friedman-Hill


You can use Gson for that:

Maven dependency:

<dependency>     <groupId>com.google.code.gson</groupId>     <artifactId>gson</artifactId>     <version>2.8.0</version> </dependency> 

Java code:

Customer customer = new Customer(); Product product = new Product();  // Set your values ...  Gson gson = new Gson(); String json = gson.toJson(customer);  Customer deserialized = gson.fromJson(json, Customer.class); 
like image 30
alexey28 Avatar answered Oct 10 '22 18:10

alexey28