1. 程式人生 > >Fastjson常用方法

Fastjson常用方法

復雜數據類型 new tty 方法 tostring listt jsonarray bject etag

Fastjson API入口類是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON類上的靜態方法直接完成。
public static final Object parse(String text); // 把JSON文本parse為JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse為JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 將JavaBean序列化為JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化為帶格式的JSON文本,boolean參數true能設置格式,false則不能
public static final Object toJSON(Object javaObject); 將JavaBean轉換為JSONObject或者JSONArray(和上面方法的區別是返回值是不一樣的)

代碼演示:

// 實體類轉換成json
    public static void voToJson() {

        UserInfo user = new UserInfo();
        user.setName("張三");
        user.setCar(null);
        user.setLike(new String[] { "吃", "喝" });
        // 註意:UserInfo中所有的屬性都會顯示出來,沒有set的以默認值的方式顯示(值為null的除外)
        String jsonstr = JSON.toJSONString(user);
        System.out.println(
"實體類轉json格式字符串 :" + jsonstr); } // list集合的轉換 public static void listToJson() { List<UserInfo> list = new ArrayList<UserInfo>(); UserInfo userinfo1 = new UserInfo(); UserInfo userinfo2 = new UserInfo(); userinfo1.setAge(12); userinfo2.setAge(
20); list.add(userinfo1); list.add(userinfo2); String json = JSON.toJSONString(list, true); System.out.println("List集合轉json格式字符串 :" + json); } // 字符數組JSON轉化為數組 public static void StringArrayToJSON() { String s = "[{\"name\":\"aa\",\"age\":10},{\"name\":\"bb\",\"age\":20}]"; List<UserInfo> list = JSON.parseArray(s, UserInfo.class); for (UserInfo ui:list) { System.out.println(ui.getName()); } } // 復雜數據類型 public static void Complexdata() { //map集合1 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("username", "zhangsan"); map.put("age", 24); map.put("sex", "男"); // map集合2 HashMap<String, Object> temp = new HashMap<String, Object>(); temp.put("name", "xiaohong"); temp.put("age", "23"); //map集合2裝map集合1中 map.put("girlInfo", temp); // list集合 List<String> list = new ArrayList<String>(); list.add("爬山"); list.add("騎車"); list.add("旅遊"); //map集合1裝了List集合 map.put("hobby", list); String jsonString = JSON.toJSONString(map); System.out.println("復雜數據類型:" + jsonString); } // JSON轉成實體類 public static void Deserialization() { String json = "{\"name\":\"cc\",\"age\":24}"; UserInfo userInfo = JSON.parseObject(json, UserInfo.class); System.out.println("姓名是:" + userInfo.getName() + ", 年齡是:" + userInfo.getAge()); } // 格式化日期 public static void DateFormate() { Date d = new Date(); System.out.println("時間:" + d); System.out.println("輸出毫秒值:" + JSON.toJSONString(d)); System.out.println("默認格式為:" + JSON.toJSONString(d, SerializerFeature.WriteDateUseDateFormat)); System.out.println("自定義日期:" + JSON.toJSONStringWithDateFormat(d, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat)); } // Json轉為實體 public static void Json2Eetity() { String json = "{\"name\":\"cc\",\"age\":24}"; UserInfo userInfo = JSON.parseObject(json, UserInfo.class); System.out.println("輸出對象的地址:" + userInfo.toString()); System.out.println("輸出對象的名字:" + userInfo.getName()); }

Fastjson常用方法