首页 > 编程知识 正文

java创建json字符串,java对象转字符串

时间:2023-05-06 03:07:45 阅读:33585 作者:3932

JSON有两种格式:对象格式和数组对象。

{'name':'JSON ',' address': '北京市西城区',' age':25}//JSON的对象格式字符串

[{'name':'JSON ',' address': '北京市西城区',' age':25}]//数据对象格式

1 .首先创建实体类

Student.java

package cn.xxs.json; 公共类student {//名称私有字符串名称; //年龄隐私字符串age; //地址私有字符串地址; public String getName () {return name; }公共语音集名称(字符串名称) {this.name=name; }公共字符串get age () {return age; }公共语音设置(stringage ) {this.age=age; }公共字符串get地址(() {return address; } publicvoidsetaddress (字符串地址) {this.address=address; }@Overridepublic String toString () {return 'Student [name=' name ',age=' age ',address=' address '] ' ); }} 2.java常规对象与json字符串的相互转换

package cn.xxs.json; /** * java常规对象与json字符串的相互转换*/import net.sf.json.JSONArray; import net.sf.json.JSONObject; 公共类测试(publicstaticvoidmain (string [ ] args ) system.out.println (---------- ) 转换对象(; system.out.println---------------- JSON字符串-----Java对象jsonStrToJava (; }公共静态语音转换对象() {Student stu=new Student ); stu.setname(JSON ); stu.setage('23 ); stu.setAddress (北京市西城区); //1,使用jsonobjectjsonobjectjson=JSON object.from object (stu ); //2,使用jsonarrayjsonarrayarray=JSON array.from object (stu ); String strJson=json.toString (; String strArray=array.toString (; system.out.println (strJSON : ) str JSON ); system.out.println (Strarray : ) strarray ); } publicstaticvoidjsonstrtojava ((/两种不同格式的字符串stringobjectstr=' {' name' :' JSON', ' age' 333333336532306; ' age':'24','address':'北京] //1,jsonobjectjsonobjectjsonobject=JSON object.from objjject studentstu=(student ) JSONobject.tobean ) JSONobject,Student.class; //2,使用jsonarrayjsonarrayjsonarray=JSON array.from object (array str ); jsonArray的第一个元素objecto=JSONArray.get(0; jsonobjectjsonobject2=JSON object.from object (o; studentstu2=(student ) JSONobject.tobean ) JSONobject2,Student.class ); system.out.println(stu: ) stu; system.out.println(stu2: ) stu2; }打印结果:

"java对象----"字符串

使用JSONObject和JSONArray两种方式转换为JSON字符串

结果显示,两种方法都可以将java对象转换为JSON字符串,但转换后的结构不同。

22

JSON字符串--》》java对象

使用JSONObject可以轻松的把JSON格式的字符串转化为java对象,但是使用JSONArray就没那么容易了,因为它有“[]”符号,所以我们这里在获得了JSONArray的对象之后,取其第一个元素即我们需要的一个student的变形,然后使用JSONObject轻松获得。

 

3.list和json字符串的互转

package cn.xxs.json;import java.util.ArrayList;import java.util.List;import net.sf.json.JSONArray;/** * list和json字符串的互转 * @author Administrator * */public class Test2 {public static void main(String[] args) {System.out.println("----------------list--》》json字符串-----------------");listToJSON();System.out.println("----------------json字符串--》》list-----------------");jsonToList();}//list--》》json字符串public static void listToJSON(){Student stu=new Student();stu.setName("JSON");stu.setAge("23");stu.setAddress("北京市海淀区");List<Student> lists=new ArrayList<Student>();lists.add(stu);//1、使用JSONObject//JSONObject listObject=JSONObject.fromObject(lists);//2、使用JSONArrayJSONArray listArray=JSONArray.fromObject(lists);//System.out.println("listObject:"+listObject.toString());System.out.println("listArray:"+listArray.toString());}//json字符串--》》listpublic static void jsonToList(){String arrayStr="[{"name":"JSON","age":"24","address":"北京市西城区"}]";//转化为listList<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);for (Student stu : list2) {System.out.println(stu);}//转化为数组Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);for (Student student : ss) {System.out.println(student);}}}

打印结果:

1》list--》》json字符串

这里把使用JSONObject的方式给注掉了,

注释之前:

有一个异常,通过查看源码发现,在使用fromObject方法的时候会先进行参数类型的判断,

这里就告诉我们,传入的参数是一个array类型,因为使用的ArrayList。

注释之后:

listArray:[{"address":"北京市海淀区","age":"23","name":"JSON"}] 这样结果是正常的。

 

2》json字符串--》》list

由于字符串的格式为带有“[]”的格式,所以这里选择JSONArray这个对象,

它有toArray、toList方法可供使用,前者转化为java中的数组,或者转化为java中的list,

由于这里有实体类进行对应,所以在使用时指定了泛型的类型(Student.class),这样就可以得到转化后的对象。

 

4.map和json字符串的互转

package cn.xxs.json;import java.util.HashMap;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;/** * map和json字符串的互转 * @author Administrator * */public class Test3 {public static void main(String[] args) {System.out.println("--------------map--》》json字符串----------------");mapToJSON();System.out.println("--------------json字符串--》》map----------------");jsonToMap();}//map--》》json字符串public static void mapToJSON(){Student stu=new Student();stu.setName("JSON");stu.setAge("23");stu.setAddress("中国上海");Map<String,Student> map=new HashMap<String,Student>();map.put("first", stu);//1、JSONObjectJSONObject mapObject=JSONObject.fromObject(map);System.out.println("mapObject"+mapObject.toString());//2、JSONArrayJSONArray mapArray=JSONArray.fromObject(map);System.out.println("mapArray:"+mapArray.toString());}//json字符串--》》mappublic static void jsonToMap(){String strObject="{"first":{"address":"中国上海","age":"23","name":"JSON"}}";//JSONObjectJSONObject jsonObject=JSONObject.fromObject(strObject);Map map=new HashMap();map.put("first", Student.class);//使用了toBean方法,需要三个参数 MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map);System.out.println(my.getFirst());}}

MyBean.java

package cn.xxs.json;public class MyBean {private Student first;public Student getFirst() {return first;}public void setFirst(Student first) {this.first = first;}}

打印结果:

1》map--》》json字符串

 

2》json字符串--》》map

JSON字符串不能直接转化为map对象,要想取得map中的键对应的值需要别的方式,

使用toBean()方法是传入了三个参数,第一个是JSONObject对象,第二个是MyBean.class,第三个是一个Map对象。通过MyBean可以知道此类中要有一个first的属性,且其类型为Student,要和map中的键和值类型对应,即,first对应键 first类型对应值的类型。

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。