fastjson轉list bean
阿新 • • 發佈:2019-02-15
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
[java] view plain copy print?
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.4</version>
</dependency>
- publicclass FastJSON {
- /**
- * @param args
- */
- publicstaticvoid main(String[] args) throws Exception{
-
//string2Json();
- //string2Object();
- //string2List();
- map2json();
- map2JSON();
- }
- /**
- * 通過fastjson把字串轉換成JSON資料
- * TypeReference
- */
- publicstaticvoid string2Json(){
- StringBuffer buffer=new StringBuffer();
- buffer.append("{");
-
buffer.append("\"age\":"
- buffer.append("\"userName\":").append("\"龍叔\"").append(",");
- buffer.append("\"address\":").append("\"廣東省雲浮市\"");
- buffer.append("}");
- String jsonText=buffer.toString();
- JSONObject jobj=JSON.parseObject(jsonText);
-
String address=jobj.get("address"
- System.out.println(address);
- }
- /**
- * 通過fastjson把字串轉換成物件
- * TypeReference
- */
- publicstaticvoid string2Object(){
- StringBuffer buffer=new StringBuffer();
- buffer.append("{");
- buffer.append("\"age\":").append("27").append(",");
- buffer.append("\"userName\":").append("\"龍叔\"").append(",");
- buffer.append("\"address\":").append("\"廣東省雲浮市\"");
- buffer.append("}");
- String jsonText=buffer.toString();
- //方法一 把json字串轉成Student物件
- Student stu1 = JSON.parseObject(jsonText, new TypeReference<Student>(){});
- //方法二 把json字串轉成Student物件
- Student stu2 = JSON.parseObject(jsonText,Student.class);
- System.out.println(stu1.getAddress());
- System.out.println(stu2.getAddress());
- }
- /**
- * 通過fastjson把字串轉換成泛型陣列
- * TypeReference
- */
- publicstaticvoid string2List(){
- StringBuffer buffer=new StringBuffer();
- buffer.append("[{");
- buffer.append("\"age\":").append("27").append(",");
- buffer.append("\"userName\":").append("\"龍叔\"").append(",");
- buffer.append("\"address\":").append("\"廣東省雲浮市\"");
- buffer.append("}]");
- String jsonText=buffer.toString();
- //轉成成陣列
- Student[] stu2 = JSON.parseObject(jsonText,new TypeReference<Student[]>(){});
- List<Student> list = Arrays.asList(stu2);
- for(Student st:list){
- System.out.println(st.getAddress());
- }
- // 轉換成ArrayList
- ArrayList<Student> list2 = JSON.parseObject(jsonText, new TypeReference<ArrayList<Student>>(){});
- for (int i = 0; i < list2.size(); i++) {
- Student obj =(Student) list2.get(i);
- System.out.println(obj.getAddress());
- }
- }
- /**
- * 通過fastjson把Map換成字串轉
- */
- publicstaticvoid map2json(){
- //建立一個Map物件
- Map<String,String> map = new HashMap<String, String>();
- map.put("username", "周伯通");
- map.put("address", "廣東省仙遊谷");
- map.put("age", "198");
- String json = JSON.toJSONString(map,true); //轉成JSON資料
- Map<String,String> map1 = (Map<String,String>)JSON.parse(json);
- //遍歷陣列資料
- for (String key : map1.keySet()) {
- System.out.println(key+":"+map1.get(key));
- }
- }
- /**
- * 通過fastjson把Map換成字串轉
- */
- publicstaticvoid map2JSON() {
- Map map = new HashMap();
- map.put("username", "周伯通");
- map.put("address", "廣東省仙遊谷");
- map.put("age", "198");
- String json = JSON.toJSONString(map);
- Map map1 = JSON.parseObject(json);
- for (Object obj : map.entrySet()) {
- Map.Entry<String, String> entry = (Map.Entry<String, String>) obj;
- System.out.println(entry.getKey() + "--->" + entry.getValue());
- }
- }
- }
[java] view plain copy print?
- package ivyy.taobao.com.entity;
- import java.io.Serializable;
- /**
- *@Author:liangjl
- *@Date:2014-12-19
- *@Version:1.0
- *@Description:
- */
- publicclass Student implements Serializable{
- private Integer age;
- private String sex;
- private String userName;
- private String birthday;
- private String address;
- private String email;
- public Integer getAge() {
- return age;
- }
- publicvoid setAge(Integer age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- publicvoid setSex(String sex) {
- this.sex = sex;
- }
- public String getUserName() {
- return userName;
- }
- publicvoid setUserName(Stri