1、JSON字符串数组转对象集合
String json ="json数组数据";JSONArray res = JSON.getJSONArray(json);//用json的方法toJavaList,参数放入想转的集合对象就可以了List monthTaskRes = res.toJavaList(MonthTaskRes.class);
2、将java对象转换为json字符串
利用json与java对象之间可以相互转换的方式进行存值和取值String s = JacksonUtils.getInstance().writeValueAsString(user);System.out.println(“对象转化字符串:”+s);User user1 = JacksonUtils.getInstance().readValue(s, User.class);System.out.println(user1 );
3、json字符串与Java对象的转换
a> 把Java对象列表转换成json对象数组,并转为字符串JSONArray array=JSONArray.fromObject(list);String jsonString = array.toString();b> 把Java对象转换成json对象,并转化成字符串JSONObject obj = JSONObject.fromObject(user);Log4jInit.ysulogger.debug(obj.toString());c> 把json字符串转换成Java对象数组JSONArray json=JSONArray.fromObject(jsonString);//jsonString字符串数组List list =(List) JSONArray .toCollection(json,User.class);d> 把字符串转换成java对象JSONObject obj = JSONObject.fromObject(jsonString);/jsonString字符串User user= (User)JSONObject.toBean(obj,User.class);
4、步骤
1、、、引入jar 包
com.alibabafastjson1.2.76
2、Java对象转成JSON格式
import com.alibaba.fastjson.JSON;import com.example.study.entity.Student;public class JsonStudy {public static void main(String[] args) {Student student = new Student("123","张三",18);//直接输出,结果为:Student(id=123, name=张三, age=18)System.out.println(student);//转换为JSON格式输出,以下两种方法只有返回值不同Object objectJson = JSON.toJSON(student);String stringJSON = JSON.toJSONString(student);//结果为:{"name":"张三","id":"123","age":18}System.out.println(objectJson);//结果为:{"name":"张三","id":"123","age":18}System.out.println(stringJSON);}}
3、JSON格式字符串转换成Java对象
3.1、直接将JSON字符串转换成Java对象
import com.alibaba.fastjson.JSONObject;import com.example.study.entity.Student;public class JsonStudy {public static void main(String[] args) {//定义一个student类型的JSON字符串String json = "{\"name\":\"张三\",\"id\":\"123\",\"age\":18}";//将这个JSON字符串转换成Student对象Student student = JSONObject.parseObject(json, Student.class);//输出结果为:Student(id=123, name=张三, age=18)System.out.println(student);//输出结果为:张三System.out.println(student.getName());}}
3.2、先将JSON字符串转换成JSON对象,再转换成Java对象
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.example.study.entity.Student;public class JsonStudy {public static void main(String[] args) {//定义一个student类型的JSON字符串String json = "{\"name\":\"张三\",\"id\":\"123\",\"age\":18}";//将这个JSON字符串转换成JSON对象JSONObject jsonObject = JSONObject.parseObject(json);//将JSON对象转换成Java对象Student student1 = JSONObject.toJavaObject(jsonObject, Student.class);Student student2 = JSON.toJavaObject(jsonObject, Student.class);//输出结果为:Student(id=123, name=张三, age=18)System.out.println(student1);//输出结果为:张三System.out.println(student1.getName());//输出结果为:Student(id=123, name=张三, age=18)System.out.println(student2);//输出结果为:张三System.out.println(student2.getName());}
3.3、如果JSON字符串是一个JSON数组,并且数组里面存放的同一种类型的对象,可以将这个JSON数组转换成Java的List对象;
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.example.study.entity.Student;import java.util.ArrayList;import java.util.List;public class JsonStudy {public static void main(String[] args) {//new一个list对象List studentList = new ArrayList();studentList.add(new Student("111","张三",18));studentList.add(new Student("222","李四",20));studentList.add(new Student("333","王五",23));//获取list的JSON数组形式字符串String listJsonString = JSON.toJSONString(studentList);//结果为:[{"age":18,"id":"111","name":"张三"},{"age":20,"id":"222","name":"李四"},{"age":23,"id":"333","name":"王五"}]System.out.println(listJsonString);//将JSON字符串转换成List对象,List里面放的Student对象List students = JSONObject.parseArray(listJsonString, Student.class);//结果为:[Student(id=111, name=张三, age=18), Student(id=222, name=李四, age=20), Student(id=333, name=王五, age=23)]System.out.println(students);//结果为:3System.out.println(students.size());//结果为:Student(id=333, name=王五, age=23)System.out.println(students.get(2));}}