fastjson的基本用法,fastjson對日期的處理

來源:互聯網
上載者:User

標籤:9.png   strong   his   com   pack   style   except   測試   util   

fastjson應該是java中對json處理效能最好的jar包了,只需要引入fastjson一個jar包即可,無需其他依賴!

下面是fastjson的一些常用的用法!

javabean 轉 JSONObject:  JSON.toJSON(javabean);  //需要強轉化

javabean的集合 轉 JSONArray:  JSON.toJSON(javabean的集合);  //需要強轉化

JSONObject 轉 javabean: JSON.toJavaObject(json,Class);

JSONArray 轉 javabean的集合:json.toJavaObject(jsonArray,List.class)  //

package com.loger.fastjson;import java.text.ParseException;import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;public class MyTest {    public static void main(String[] args) throws ParseException {        User user = new User("007","陳樂");        List<User> list = new ArrayList<>();        list.add(user);        list.add(user);        JSONObject o = new JSONObject();        o = (JSONObject) JSON.toJSON(user);        System.out.println("測試javabean變JSONObject:\n"+o);        System.out.println("--------------------");        User user2 = JSON.toJavaObject(o, User.class);        System.out.println("測試JSONObject變javabean:\n"+user2);        System.out.println("--------------------");        JSONArray jsonArray = (JSONArray) JSON.toJSON(list);        System.out.println("測試javalist變jsonarray:\n"+jsonArray);        System.out.println("-------------");        List<User> list2 = JSON.toJavaObject(jsonArray, List.class);        System.out.println("jsonarray變javabean集合"+list2.toString());        System.out.println("----------------");    }}

運行結果:

 

fastjson對日期的處理:

javabean:

package com.loger.fastjson;import java.util.Date;import com.alibaba.fastjson.annotation.JSONField;public class UserWithDate {        private String name;    @JSONField(format="yyyy-mm-dd")    private Date date;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }    public UserWithDate(String name, Date date) {        super();        this.name = name;        this.date = date;    }    public UserWithDate() {        super();    }    @Override    public String toString() {        return "UserWithDate [name=" + name + ", date=" + date + "]";    }        }

Main.java

package com.loger.fastjson;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;public class TestForDate {        public static void main(String[] args) throws ParseException {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");        Date date = sdf.parse("1995-09-01");        UserWithDate user = new UserWithDate("陳樂",date);        JSONObject o = (JSONObject) JSON.toJSON(user);        System.out.println(o);        System.out.println("------------------");        UserWithDate user2 = new UserWithDate();        user2 = JSON.toJavaObject(o, UserWithDate.class);        System.out.println(user2);    }    }

結果:

fastjson的基本用法,fastjson對日期的處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.