您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“JSON和Javabean如何实现互转”,在日常操作中,相信很多人在JSON和Javabean如何实现互转问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JSON和Javabean如何实现互转”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
JSONObject:
{
"area": "武汉",
"name": "张三",
"age": 25
}
JSONArray:
[{
“area”: “武汉”,
“name”: “张三”,
“age”: 25
},
{
“area”: “深圳”,
“name”: “李四”,
“age”: 22
}]
通俗来讲 JSONObject 是对象的json形式 JSONArry 是对象集合的JSON形式。
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;
}
}
方法一:通过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);
}
}
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"));
}
}
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));
}
}
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());
到此,关于“JSON和Javabean如何实现互转”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。