介绍几种JSON和Javabean互转的常用方法 Java代码实现
2021-07-16 09:40:59
浏览数 (8441)
JSON因为其强大特性的原因,是现阶段web开发中前后端交互的最主要的数据格式。后端开发中经常使用到Javabean,因此时常需要将JSON和Javabean进行互相转换。本文将为您介绍几种关于如何应用 Java 语言来实现转换JSON和Javabean的常用方法。
JSONObject 与 JSONArray区别
JSONObject:
{
"area": "武汉",
"name": "张三",
"age": 25
}
JSONArray:
[{
“area”: “武汉”,
“name”: “张三”,
“age”: 25
},
{
“area”: “深圳”,
“name”: “李四”,
“age”: 22
}]
通俗来讲 JSONObject 是对象的json形式 JSONArry 是对象集合的JSON形式。
JSON 与javabean互转
JSON用阿里的fastjson 包
用例java对象
public class User {
protected Long id;
protected String account;
protected String password;
protected String name;
protected boolean gender;
protected String telephone;
@Override
public String toString() {
return "User{" +
"id=" + id +
", account='" + account + ''' +
", password='" + password + ''' +
", name='" + name + ''' +
", gender=" + gender +
", telephone='" + telephone + ''' +
'}';
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
1、javabean转json
方法一:通过java对象转成String再转成JSONObject
package com.handoop.gms.utils;
import com.alibaba.fastjson.JSONObject;
import com.handoop.gms.domain.User;
public class TestMain {
public static void main(String []args){
//先通过构造函数初始化一个对象
User user=new User((long) 1,"admin","admin","张三",true,"123456");
//先将java对象转为String类型
String jsonString= JSONObject.toJSONString(user);
//再将String类型转为JSONObject
JSONObject jsonObject=JSONObject.parseObject(jsonString);
System.out.println(jsonObject);
//转为JSONObject后就可以随时根据键值获取他的元素了
System.out.println(jsonObject.get("password"));
}
}
运行结果
方法2:java对象直接转json
package com.handoop.gms.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handoop.gms.domain.User;
public class TestMain {
public static void main(String []args){
//先通过构造函数初始化一个对象
User user=new User((long) 1,"admin","admin","张三",true,"123456");
JSONObject jsonObject= (JSONObject) JSONObject.toJSON(user);
System.out.println(jsonObject);
}
}
运行结果
json字符串转JSONObeject
public class TestMain {
public static void main(String []args){
String str="{"password":"admin","gender":true,"name":"张三","telephone":"123456","id":1,"account":"admin"}";
JSONObject jsonObject=JSONObject.parseObject(str);
System.out.println("account: "+jsonObject.get("account")+"---"+"paasword: "+jsonObject.get("password"));
}
}
运行结果
3.jsonString 转JSONArray
public class TestMain {
public static void main(String []args){
String str="{"data":[{"password":"admin","gender":true,"name":"张三","telephone":"123456","id":1,"account":"admin"}]}";
//先转成JSONObject
JSONObject jsonObject=JSONObject.parseObject(str);
//再将JSONObject中数组类型数据取出转成JSONArray
JSONArray jsonArray=jsonObject.getJSONArray("data");
System.out.println(jsonArray.get(0));
}
}
运行结果
4.JSON字符串转JAVA对象
String str="{"password":"admin","gender":true,"name":"张三","telephone":"123456","id":1,"account":"admin"}";
// 前面是JSON字符串 后面是java对象类型
User user=JSONObject.parseObject(str,User.class);
System.out.println("account: "+user.getAccount()+"---"+"paasword: "+user.getPassword());
输出结果
以上就是使用 Java代码实现JSON和Javabean互转的几种常用方法的全部内容 ,想要了解更多相关 Java 的内容请搜索W3Cschool以前的文章或继续浏览下面的相关文章!