Convert Java object to Json and vice versa?

I know that a JSON object is nothing but a String .

My question is that I have a Map of Object and I want to convert it to Json format.

Example:

 Java Class -> Class Person{ private String name; private String password; private int number; } Java list -> Map<List<Long>,List<Person>> map=new HashMap<List<Long>,List<Person>>(); ..and map has Some data filled in it. 

I want to convert this list to

  Json Format? 

How can i achieve this? Because I want to send it via HttpClient ... If there is no other alternative way?

As far as I know, there is a Gson API , but I don’t know how to use it in another effective way.

thanks

+4
source share
4 answers

I got my answer, but thanks for your answers.

  Map<Long,List<Person>> map=new HashMap<Long,List<Person>>(); //adding some data Gson gson=new Gson(); String mapJsonStr=gson.toJson(map); //mapJsonStr : is my map JSon Strin 

for the opposite

  TypeToken<Map<Long,List<Person>>> token = new TypeToken<Map<Long,List<Person>>>(){}; Map<Long,List<Person>> map_new=new HashMap<Long,List<Person>>(); map_new=gson.fromJson(mapJsonStr,token.getType()); //origian map // map_new is my Map get from map Json String 

This is it.Thank you

+1
source

I don’t know what the problem is with Gson. From the doc :

 BagOfPrimitives obj = new BagOfPrimitives(); Gson gson = new Gson(); String json = gson.toJson(obj); 

and

 BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class); 

This object (as the name implies) consists of primitives. However, Gson will trivially process objects, collections of objects, etc. Life gets a little more complicated when using generics, etc., but for your example above, I would expect Gson to work with small problems.

+4
source

Using Gson to convert to Json using Gson on the client side.

Sending an array of String.

  String[] subscriberArray = new String[]{"eee", "bbb"}; Gson gson = new Gson(); String recipientInfoStringFormat = gson.toJson(subscriberArray); 

Sending an array of a user-defined type.

  RecipientInfo[] recipientInfos = new RecipientInfo[1]; RecipientInfo ri = new RecipientInfo(); ri.setA(1); ri.setB("ss"); recipientInfos.add(ri); Gson gson = new Gson(); String recipientInfoStringFormat = gson.toJson(recipientInfos); 

Using server-side Gson to read data.

For primitive types.

  String subscriberArrayParam = req.getParameter("subscriberArrayParam"); Gson gson = new Gson(); String[] subscriberArray = gson.fromJson(subscriberArrayParam, String[].class); for (String str : subscriberArray) { System.out.println("qq :"+str); } 

For custom object

  String recipientInfos = req.getParameter("recipientInfoStringFormat"); Gson gson = new Gson(); RecipientInfo[] ri = gson.fromJson(recipientInfos, RecipientInfo[].class); 
+3
source

You can also use jackson.

  Person person= new Person(); ObjectMapper mapper = new ObjectMapper(); try { // convert personobject to json string, and save to a file mapper.writeValue(new File("c:\\person.json"), person); // display to console System.out.println(mapper.writeValueAsString(person)); } catch (Exception e) { e.printStackTrace(); } 

and vice versa

  ObjectMapper mapper = new ObjectMapper(); try { // read from file, convert it to user class Person person= mapper.readValue(new File("c:\\person.json"), Person.class); // display to console System.out.println(person); } catch (Exception e) { e.printStackTrace(); } 

to use jackson add this dependency to your POM.xml

 <repositories> <repository> <id>codehaus</id> <url>http://repository.codehaus.org/org/codehaus</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> </dependency> </dependencies> 
+1
source

Source: https://habr.com/ru/post/1495762/


All Articles