JSON格式的数据传递是最常用的方法之一,以下列出了常用的几种形态以及与Javabean之间的转换:
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}";
首先,此处的转化依赖两个JAR包
com.google.code.gson gson2.8.1 org.json json20170516
其次,封装的Javabean代码如下
import java.util.List;
public class UserBean {
private String name;
private Integer age;
private List 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 getInterests() {
return interests;
}
public void setInterests(List interests) {
this.interests = interests;
}
class InterestBean{
private String interest;
private List colors;
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public List getColors() {
return colors;
}
public void setColors(List colors) {
this.colors = colors;
}
}
}
1、普通的json4格式的JSON解析:
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解析以及GSON解析:
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);
}
}
public void testParseListJson(){
Gson gson = new Gson();
Type type = new TypeToken>(){}.getType();
List users = gson.fromJson(json2, type);
for(UserBean user:users){
System.out.println(user.getName());
}
}
3、内嵌JSON形式的JSON与GSON解析:
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);
}
}
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 list = user.getInterests();
for(InterestBean bean:list) {
System.out.println(bean.getInterest());
List colors = bean.getColors();
for(String color:colors){
System.out.println(color);
}
}
}
4、Map形式的JSON的GSON解析:
public void testParseMapJson(){
Gson gson = new Gson();
Type type = new TypeToken
5、将一个JavaBean对象封装成JSON格式
public String testJavaBean2Json(){
UserBean userBean = new UserBean();
userBean.setName("zhangsan");
userBean.setAge(33);
List list = new ArrayList();
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;
}
}
仅供参考,如有雷同,纯属巧合^_^
以上这篇详谈JSON与Javabean转换的几种形式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



