阿里的fastJSON的用法
来源:互联网 发布:mac office2016破解版 编辑:程序博客网 时间:2024/05/17 06:18
新建两个实体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); }}
阅读全文
0 0
- 阿里的fastJSON的用法
- 阿里的FastJson
- 阿里FastJson的使用
- 阿里fastjson的使用实例
- 阿里的fastjson使用总结
- FastJSON的用法
- 来自阿里的 json 解析方案 fastjson
- alibaba的fastjson的用法
- fastjson的一些常见用法
- fastjson的用法及介绍
- FastJson用法的简单案例
- 阿里的 com.alibaba.fastjson的日常使用
- fastjson中@JSONField注解的用法
- json、gson、fastjson的简单用法
- 映入眼帘的JSON-fastjson常见用法
- 使用最快捷的 FastJson-json用法
- FastJson-fastjson的简单使用
- fastjson中SerializerFeature的用法及中文注解
- 浅谈JavaScript中forEach与each
- Spring配置报错:通配符的匹配很全面,但无法找到元素‘util:list’的声明
- YARN框架解析
- java基础—JVM的垃圾算法有哪几种?CMS垃圾回收的基本流程?
- 深入理解Java中的final关键字
- 阿里的fastJSON的用法
- Haxe手册摘要 一. 类型
- 技术应该怎么学习
- 医院客户关系系统
- 单例模式
- shiro之web配置
- 详述支付路由的设计方案
- 版本控制之道之SVN使用教程(常用命令)
- php Interpreter is not configured