alibaba之fastJson常见应用
案例一:
json格式字符串转JSONObject对象
package com.sr.biofunet.web.controller.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class Test { public static void main(String[] args) { String jsonData = "{\"id\":\"1\",\"name\":\"张三\"}"; JSONObject jsonObject = JSON.parseObject(jsonData); System.out.println("key:" + jsonObject.getString("id")); System.out.println("key:" + jsonObject.get("name")); } }
运行结果
key:1
key:张三
案例二 :
java对象转JSONObject对象
package com.sr.biofunet.web.controller.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.text.SimpleDateFormat; import java.util.Date; public class Test { public static void main(String[] args) { Student s = new Student(); s.setId(1); s.setName("李四"); s.setBirthday(new Date()); String jsonData = JSON.toJSONString(s); JSONObject jsonObject = JSON.parseObject(jsonData); System.out.println(jsonObject.get("name")); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(jsonObject.getDate("birthday"))); } }
运行结果:
李四
2017-08-01 15:42:03
案例三:
Map对象转JSONObject对象
package com.sr.biofunet.web.controller.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.util.Date; import java.util.HashMap; public class Test { public static void main(String[] args) { HashMap<String,String> hashMap = new HashMap<>(); hashMap.put("a","A"); hashMap.put("b","B"); JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(hashMap)); System.out.println(jsonObject.getString("a")); System.out.println(jsonObject.getString("b")); } }
运行结果
A
B
案例四:
java对象转换JSONObject对象跟@JSONField注解的使用
次注解的作用是输出json格式数据的时候重新定义json中key的别名
package com.sr.biofunet.web.controller.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class Test { public static void main(String[] args) { Student student = new Student(); student.setBirthday(new Date()); JSONObject jsonObject = (JSONObject)JSON.toJSON(student); System.out.println(jsonObject.getDate("birthday")); System.out.println(jsonObject.getDate("json_birthday")); } } class Student{ private Integer id; private String name; private double weight; @JSONField(name="json_birthday") private Date birthday; private char sex; public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public Student(Integer id, String name, double weight, Date birthday, char sex) { this.id = id; this.name = name; this.weight = weight; this.birthday = birthday; this.sex = sex; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", weight=" + weight + ", birthday=" + birthday + ", sex=" + sex + '}'; } }
输出结果:
null
Tue Aug 01 16:09:12 CST 2017