阿里的fastJSON的用法
2017-12-06 14:08
232 查看
新建两个实体User和Teacher
package org.fastjson; public class User { private int id; private String userName; public User() { } public User(int id, String userName) { this.id = id; this.userName = userName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + "]"; } }
package org.fastjson; public class Teacher { private int id; private String classNo; private String courseNo; public Teacher() { super(); } public Teacher(int id, String classNo, String courseNo) { this.id = id; this.classNo = classNo; this.courseNo = courseNo; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getClassNo() { return classNo; } public void setClassNo(String classNo) { this.classNo = classNo; } public String getCourseNo() { return courseNo; } public void setCourseNo(String courseNo) { this.courseNo = courseNo; } @Override public String toString() { return "Teacher [id=" + id + ", classNo=" + classNo + ", courseNo=" + courseNo + "]"; } }
package org.fastjson; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; public class FastjsonClass { public static void main(String[] args) { System.out.println(mapToJson()); } public void parseJson(String jsonStr){ User user = new User(11,"京津冀"); Teacher teacher = new Teacher(1,"A1000","CC2000"); Teacher teacher1 = new Teacher(2,"A1001","CC2001"); List<Teacher> teacherList = new ArrayList<Teacher>(); teacherList.add(teacher); teacherList.add(teacher1); Temp temp = new Temp(user,teacherList); String tempJson = JSON.toJSONString(temp); System.out.println(tempJson); JSONObject jsonObj = JSON.parseObject(tempJson); JSONArray result = jsonObj.getJSONArray("teacherList"); List<Teacher> th = JSON.parseArray(result.toJSONString(),Teacher.class); System.out.println(th.toString()); JSONObject userObj = jsonObj.getJSONObject("user"); User user1 = JSON.parseObject(userObj.toJSONString(), new TypeReference<User>() {}); User user2 = JSON.parseObject(userObj.toJSONString(), User.class); System.out.println(user.toString()); try { DButils.getConnection(); // System.out.println(getCount("SELECT count(table_name) FROM information_schema.TABLES WHERE table_name='book' ")); DButils.insertEntity(User.class, user2); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String mapToJson(){ Map<String, Object> map = new HashMap<String, Object>(); User user = new User(11,"京津冀"); map.put("user", user); Teacher teacher = new Teacher(1,"A1000","CC2000"); Teacher teacher1 = new Teacher(2,"A1001","CC2001"); List<Teacher> teacherList = new ArrayList<Teacher>(); teacherList.add(teacher); teacherList.add(teacher1); map.put("teacherList", teacherList); return JSON.toJSONString(map); } }
相关文章推荐
- Fastjon JSONField 注解 常见用法
- FastJson--阿里开源的速度最快的Json和对象转换工具
- FastJson基本用法
- 在 springmvc 中, 如何配置 fastjson (阿里开源的) 和 Jackson( Spring 内置的)
- Json转换工具类(基于google的Gson和阿里的fastjson)
- 生成JSON数据--fastjson(阿里)方法
- FastJson的基本用法----转
- fastjson的一些常见用法
- alibaba的fastjson的用法
- 用阿里的fastJson.jar进行json数据与list互转
- 阿里JSon开源组件fastjson应用
- FastJson和Gson和Json数据解析分析和用法
- Java_Json转换_FastJson 用法调研
- 阿里fastjson 对象转JSON
- fastjson的用法及介绍
- fast-json.jar的用法
- 映入眼帘的JSON-fastjson常见用法
- 阿里的 com.alibaba.fastjson的日常使用
- json、gson、fastjson的简单用法
- fastjson-1.1.34.android,和xUtils-2.6.13简单用法1,将网络新闻转化到app