赞
踩
String json ="json数组数据";
JSONArray res = JSON.getJSONArray(json);
//用json的方法toJavaList,参数放入想转的集合对象就可以了
List<MonthTaskRes> monthTaskRes = res.toJavaList(MonthTaskRes.class);
利用json与java对象之间可以相互转换的方式进行存值和取值
String s = JacksonUtils.getInstance().writeValueAsString(user);
System.out.println(“对象转化字符串:”+s);
User user1 = JacksonUtils.getInstance().readValue(s, User.class);
System.out.println(user1 );
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<User> list =(List<User>) JSONArray .toCollection(json,User.class); d> 把字符串转换成java对象 JSONObject obj = JSONObject.fromObject(jsonString);/jsonString字符串 User user= (User)JSONObject.toBean(obj,User.class);
1、、、引入jar 包
<!-- fastjson依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
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<Student> 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<Student> 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); //结果为:3 System.out.println(students.size()); //结果为:Student(id=333, name=王五, age=23) System.out.println(students.get(2)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。