In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the use of json format conversion in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. the interchange between javaBean and json
JavaBean class
Public class Person {private Integer id; private String name; public Person () {} public Person (Integer id, String name) {this.id = id; this.name = name;} public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} @ Override public String toString () {return "Person {" + "id=" + id + ", name='" + name +''+'}';}}
JsonTest test class
@ Test public void test1 () {Person person = new Person (1, "Zhang San"); Gson gson = new Gson (); String personJsonToString = gson.toJson (person); / / toJson () method can convert java object to json string System.out.println (personJsonToString); Person person1 = gson.fromJson (personJsonToString, Person.class) The / / fromJson () method converts a json string into a java object System.out.println (person1);}
2. The interchange between List and json
Public class PersonListType extends TypeToken {} @ Test public void test2 () {ArrayList personArrayList = new ArrayList (); personArrayList.add (new Person (1, "Zhang San"); personArrayList.add (new Person (2, "Li Si")); personArrayList.add (new Person (3, Wang Wu)); Gson gson = new Gson (); / / convert List to json string String personListToString = gson.toJson (personArrayList) System.out.println (personListToString); / / convert the json string to List List list = gson.fromJson (personListToString, new PersonListType (). GetType ()); System.out.println (list); Person person = list.get (0); System.out.println (person);}
If you directly personArrayList.getClass (), convert the json string to List, and then take the first element of List for output, an exception will occur
/ / List list = gson.fromJson (personListToString, new PersonListType (). GetType ()); List list = gson.fromJson (personListToString, personArrayList.getClass ()); System.out.println (list); System.out.println (list.get (0) instanceof Person); System.out.println (list.get (0) instanceof List); System.out.println (list.get (0) instanceof Map); Person person = list.get (0) System.out.println (person)
The output is as follows:
From the result, we can see that the element in the List converted by json is not a Person object or a List object, but a Map object, so forcibly converting the Person object will report an error.
If you do not convert a json string to an List collection, and every element in the collection is an object of type Person, the second parameter must use the type Type.
Create a new PersonListType class that inherits from the TypeToken class
/ / TypeToken is a generic class. Either List or concrete ArrayList can public class PersonListType extends TypeToken {} List list = gson.fromJson (personListToString, new PersonListType (). GetType ()); / / List list = gson.fromJson (personListToString, personArrayList.getClass ()); System.out.println (list); System.out.println (list.get (0) instanceof Person); System.out.println (list.get (0) instanceof List) System.out.println (list.get (0) instanceof Map); Person person = list.get (0); System.out.println (person)
III. The interchange between map and json
PersonMapType class, there is a return JavaBean can directly. GetClass (), and if the return is a collection object, you need to inherit the TypeToken class.
Public class PersonMapType extends TypeToken {} @ Test public void test3 () {Map map = new HashMap (); map.put (1 Person (1, "Zhang San"); map.put (2 Person (2, "Li Si")); Gson gson = new Gson (); / / convert the map collection into the json string String personMapJsonString = gson.toJson (map); System.out.println (personMapJsonString) / / convert json strings into map collections Map personMap1 = gson.fromJson (personMapJsonString, new PersonMapType (). GetType ()); System.out.println (personMap1); Person person = personMap1.get (1); System.out.println (person);} Thank you for reading! This is the end of the article on "what is the use of json format in Java?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.