The following editor will bring you an article that discusses several forms of JSON and Javabean conversion in detail. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
JSON format data transfer is one of the most commonly used methods. The following lists several commonly used forms and conversions with Javabeans:
String json1="{'name':'zhangsan','age':23,'interests':[{'interest':'篮球','colors':['绿色','黄色']},{'interest':'足球','colors':['红色','蓝色']}]}"; String json2="[{'name':'zhangsan'},{'name':'lisi'},{'name':'王五'}]"; String json3="{'1':{'name':'zhangsan'},'3':{'name':'lisi'},'4':{'name':'wangwu'}}";//map String json4="{'name':'zhangsan','age':23}";
First of all, the transformation here relies on two JAR packages
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency>
Secondly, the encapsulated Javabean code is as follows
import java.util.List; public class UserBean { private String name; private Integer age; private List<InterestBean> interests; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public List<InterestBean> getInterests() { return interests; } public void setInterests(List<InterestBean> interests) { this.interests = interests; } class InterestBean{ private String interest; private List<String> colors; public String getInterest() { return interest; } public void setInterest(String interest) { this.interest = interest; } public List<String> getColors() { return colors; } public void setColors(List<String> colors) { this.colors = colors; } } }
1. JSON parsing in ordinary json4 format:
public void testParseJson(){ JSONObject jsonObj = new JSONObject(json4); String name = jsonObj.getString("name"); int age = jsonObj.getInt("age"); System.out.println(name); System.out.println(age); UserBean user = new UserBean(); user.setAge(age); user.setName(name); }
2. JSON parsing in array form and GSON parsing:
public void testJsonArray(){ JSONArray jsonArray = new JSONArray(json2); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); String name = jsonObj.getString("name"); System.out.println(name); } }
/** * 解析json数组 */ public void testParseListJson(){ Gson gson = new Gson(); Type type = new TypeToken<List<UserBean>>(){}.getType(); List<UserBean> users = gson.fromJson(json2, type); for(UserBean user:users){ System.out.println(user.getName()); } }
3. JSON and GSON parsing in embedded JSON form:
/** * 内嵌JSON解析 */ public void testParseJson1(){ JSONObject rootJson = new JSONObject(json1); JSONArray jsonInterestArray = rootJson.getJSONArray("interests"); for (int i = 0; i < jsonInterestArray.length(); i++) { JSONObject interestJsonObj = jsonInterestArray.getJSONObject(i); String interest = interestJsonObj.getString("interest"); System.out.println(interest); Object obj = interestJsonObj.get("colors"); System.out.println(obj); } }
/** * 内嵌GSON解析 */ public void testSimpleJson(){ Gson gson = new Gson(); UserBean user = gson.fromJson(json1, UserBean.class); System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getInterests().size()); List<InterestBean> list = user.getInterests(); for(InterestBean bean:list) { System.out.println(bean.getInterest()); List<String> colors = bean.getColors(); for(String color:colors){ System.out.println(color); } } }
4. GSON parsing of JSON in Map form:
/** * 解析一个map类型的json */ public void testParseMapJson(){ Gson gson = new Gson(); Type type = new TypeToken<Map<String,UserBean>>(){}.getType(); Map<String,UserBean> map = gson.fromJson(json3, type); Set<String> keys = map.keySet(); for(String key:keys){ UserBean bean = map.get(key); System.out.println(key); System.out.println(bean.getName()); } }
5. Encapsulate a JavaBean object into JSON format
/** * 将一个JavaBean对象封装成JSON格式 */ public String testJavaBean2Json(){ UserBean userBean = new UserBean(); userBean.setName("zhangsan"); userBean.setAge(33); List<InterestBean> list = new ArrayList<InterestBean>(); InterestBean bean1 = new UserBean().new InterestBean(); bean1.setInterest("篮球1"); InterestBean bean2 = new UserBean().new InterestBean(); bean2.setInterest("篮球2"); list.add(bean1); list.add(bean2); userBean.setInterests(list); //将User Bean转换成Json Gson gson = new Gson(); String jsonStr = gson.toJson(userBean); System.out.println(jsonStr); return jsonStr; } }
The above is the detailed content of Introduction to several forms of Javabean and JSON conversion. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
