FastJson 解析JSON/映射实体

来源:互联网 发布:js中的this详解 编辑:程序博客网 时间:2024/05/29 14:26

fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。

主要特点:

  • 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)

  • 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)

  • 零依赖(没有依赖其它任何类库除了JDK)


Github : https://github.com/alibaba/fastjson


JSON解析:
public class FastJsonMain {static String json = "{\"study\": 10,\"study_course_count\": 10, \"recieved_reg_form_count\": 0,\"unread_count\": 5,\"teach_course_count\": 8,\"avatar\": \"1316634098115-02-57\",\"user_id\": 201,\"nickname\": \"Asus人名\"}";static String arrayAyy="[[14,\"小学语文\"],[154,\"美食\"],[72,\"高中物理\"],null,[50,\"高中化学\"],[15,\"小学数学\"],[13\"英语\"],null,[1,\"其他英语培训\"],null]";/** * Json 转换 实体类 提示:FastJson在映射实体上非常棒, json有的K,实体没有,依然不影响解析.. */private void Json2Eetity() {// TODO Auto-generated method stubWelcome welcome = JSON.parseObject(json, Welcome.class);System.out.println(welcome.toString());System.out.println(welcome.getNickname());}/** * 实体类 转换 Json */private void Eetity2Json() {// TODO Auto-generated method stubWelcome welcome = new Welcome(2, 3, 4, 5, "imagUrl", 11, "Mers");String json = JSON.toJSONString(welcome, true);System.out.println("实体转换Json:" + json);}/** * list集合转json格式字符串 */public void list2Json() {List<Welcome> list = new ArrayList<Welcome>();Welcome welcome1 = new Welcome(2, 3, 4, 5, "imagUrl", 11, "Mers");Welcome welcome2 = new Welcome(22, 33, 44, 55, "imag", 65, "Kers");Welcome welcome3 = new Welcome(64, 33, 34, 05, "imagUrl", 43, "Wers");Welcome welcome4 = new Welcome(62, 75, 41, 25, "imagUrl", 109, "Oers");list.add(welcome1);list.add(welcome2);list.add(welcome3);list.add(welcome4);String json = JSON.toJSONString(list, true);System.out.println("ist集合转json格式字符串 :" + json);}/** * String转换 JSONArray * 并且去除null */private void String2JSONArray() {// TODO Auto-generated method stubJSONArray array=JSONArray.parseArray(arrayAyy);System.out.println(array);System.out.println("长度: "+array.size());Collection nuCon = new Vector(); nuCon.add(null); array.removeAll(nuCon);System.out.println(array);System.out.println("长度: "+array.size());}public static void main(String[] args) {FastJsonMain main = new FastJsonMain();main.Json2Eetity();System.out.println(" ");main.Eetity2Json();System.out.println("");main.list2Json();System.out.println(" ");main.String2JSONArray();}}


以上fastjson-1.1.34:FastJson(源码+jar+Dome)
0 0
原创粉丝点击