这篇文章主要介绍“JSON和Javabean如何实现互转”,在日常操作中,相信很多人在JSON和Javabean如何实现互转问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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对象

publicclassUser{protectedLongid;protectedStringaccount;protectedStringpassword;protectedStringname;protectedbooleangender;protectedStringtelephone;@OverridepublicStringtoString(){return"User{"+"id="+id+",account='"+account+'''+",password='"+password+'''+",name='"+name+'''+",gender="+gender+",telephone='"+telephone+'''+'}';}publicbooleanisGender(){returngender;}publicvoidsetGender(booleangender){this.gender=gender;}publicStringgetTelephone(){returntelephone;}publicvoidsetTelephone(Stringtelephone){this.telephone=telephone;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}publicStringgetAccount(){returnaccount;}publicvoidsetAccount(Stringaccount){this.account=account;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}}1、javabean转json

方法一:通过java对象转成String再转成JSONObject

packagecom.handoop.gms.utils;importcom.alibaba.fastjson.JSONObject;importcom.handoop.gms.domain.User;publicclassTestMain{publicstaticvoidmain(String[]args){//先通过构造函数初始化一个对象Useruser=newUser((long)1,"admin","admin","张三",true,"123456");//先将java对象转为String类型StringjsonString=JSONObject.toJSONString(user);//再将String类型转为JSONObjectJSONObjectjsonObject=JSONObject.parseObject(jsonString);System.out.println(jsonObject);//转为JSONObject后就可以随时根据键值获取他的元素了System.out.println(jsonObject.get("password"));}}

方法2:java对象直接转json

packagecom.handoop.gms.utils;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importcom.handoop.gms.domain.User;publicclassTestMain{publicstaticvoidmain(String[]args){//先通过构造函数初始化一个对象Useruser=newUser((long)1,"admin","admin","张三",true,"123456");JSONObjectjsonObject=(JSONObject)JSONObject.toJSON(user);System.out.println(jsonObject);}}json字符串转JSONObeject

publicclassTestMain{publicstaticvoidmain(String[]args){Stringstr="{"password":"admin","gender":true,"name":"张三","telephone":"123456","id":1,"account":"admin"}";JSONObjectjsonObject=JSONObject.parseObject(str);System.out.println("account:"+jsonObject.get("account")+"---"+"paasword:"+jsonObject.get("password"));}}3.jsonString 转JSONArray

publicclassTestMain{publicstaticvoidmain(String[]args){Stringstr="{"data":[{"password":"admin","gender":true,"name":"张三","telephone":"123456","id":1,"account":"admin"}]}";//先转成JSONObjectJSONObjectjsonObject=JSONObject.parseObject(str);//再将JSONObject中数组类型数据取出转成JSONArrayJSONArrayjsonArray=jsonObject.getJSONArray("data");System.out.println(jsonArray.get(0));}}4.JSON字符串转JAVA对象

Stringstr="{"password":"admin","gender":true,"name":"张三","telephone":"123456","id":1,"account":"admin"}";//前面是JSON字符串后面是java对象类型Useruser=JSONObject.parseObject(str,User.class);System.out.println("account:"+user.getAccount()+"---"+"paasword:"+user.getPassword());

到此,关于“JSON和Javabean如何实现互转”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!