JSON

Gson和Fastjson的使用(2)

字号+ 作者:H5之家 来源:H5之家 2017-08-03 10:02 我要评论( )

Gson gson = new Gson(); 这样的对象默认对Html进行转义,假设不想转义使用以下的方法 GsonBuilder builder = new GsonBuilder();builder.disableHtmlEscaping();Gson gson = builder. create (); 二、阿里巴巴的Fa

Gson gson = new Gson();
这样的对象默认对Html进行转义,假设不想转义使用以下的方法

GsonBuilder builder = new GsonBuilder(); builder.disableHtmlEscaping(); Gson gson = builder.create(); 二、阿里巴巴的FastJson包的使用简单介绍。 (1)maven依赖

com.alibaba
fastjson
1.2.7
开源地址 https://github.com/alibaba/fastjson

(2)基础转换类

同上

(3)bean转换json

将对象转换成格式化的json
JSON.toJSONString(obj,true);
将对象转换成非格式化的json
JSON.toJSONString(obj,false);
obj设计对象
对于复杂类型的转换,对于反复的引用在转成json串后在json串中出现引用的字符,比方
$ref":"$[0].books[1]

Student stu = new Student(); Set books= new HashSet(); Book book = new Book(); books.add(book); stu.setBooks(books); ArrayList(); for(int i;i++) list.add(stu); String json = JSON.toJSONString(list,true); (4)json转换bean

String json = "{\"id\":\"2\",\"name\":\"Json技术\"}";
Book book = JSON.parseObject(json, Book.class);

(5)json转换复杂的bean,比方List,Map //将json转换成List List list = JSON.parseObject(json,new TypeReference<ARRAYLIST>(){}); //将json转换成Set Set set = JSON.parseObject(json,new TypeReference<HASHSET>(){}); (6)通过json对象直接操作json a)从json串中获取属性 String propertyName = ‘id‘; String propertyValue = ""; String json = "{\"id\":\"1\",\"name\":\"Json技术\"}"; JSONObject obj = JSON.parseObject(json); propertyValue = obj.get(propertyName)); b)除去json中的某个属性 String propertyName = ‘id‘; String propertyValue = ""; String json = "{\"id\":\"1\",\"name\":\"Json技术\"}"; JSONObject obj = JSON.parseObject(json); Set set = obj.keySet(); propertyValue = set.remove(propertyName); json = obj.toString(); c)向json中加入属性 String propertyName = ‘desc‘; Object propertyValue = "json的玩意儿"; String json = "{\"id\":\"1\",\"name\":\"Json技术\"}"; JSONObject obj = JSON.parseObject(json); obj.put(propertyName, JSON.toJSONString(propertyValue)); json = obj.toString(); d)改动json中的属性 String propertyName = ‘name‘; Object propertyValue = "json的玩意儿"; String json = "{\"id\":\"1\",\"name\":\"Json技术\"}"; JSONObject obj = JSON.parseObject(json); Set set = obj.keySet(); if(set.contains(propertyName)) obj.put(propertyName, JSON.toJSONString(propertyValue)); json = obj.toString(); e)推断json中是否有属性 String propertyName = ‘name‘; boolean isContain = false; String json = "{\"id\":\"1\",\"name\":\"Json技术\"}"; JSONObject obj = JSON.parseObject(json); Set set = obj.keySet(); isContain = set.contains(propertyName); f)json中日期格式的处理 Object obj = new Date(); String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss.SSS");

使用JSON.toJSONStringWithDateFormat,该方法能够使用设置的日期格式对日期进行转换

Gson和Fastjson的使用

标签:                           

原文:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • FastJson 介绍

    FastJson 介绍

    2017-08-02 16:00

  • 【转】JMeter学习(三十五)使用jmeter来发送json

    【转】JMeter学习(三十五)使用jmeter来发送json

    2017-08-01 16:02

  • Gson读写JSON 数据

    Gson读写JSON 数据

    2017-07-31 09:00

  • [C++11版]Ubuntu下Json的使用

    [C++11版]Ubuntu下Json的使用

    2017-07-30 10:02

网友点评