当前位置: 首页 > news >正文

o2o网站建设哪家好二级域名免费分发

o2o网站建设哪家好,二级域名免费分发,团购网站大全做相册,做一家网站费用吗在实际开发中,我们对于 JSON 数据的处理,通常有这么几个第三方工具包可以使用: gson:谷歌的fastjson:阿里巴巴的jackson:美国FasterXML公司的,Spring框架默认用的 由于以前一直用习惯了阿里的…

在实际开发中,我们对于 JSON 数据的处理,通常有这么几个第三方工具包可以使用:

  • gson:谷歌的
  • fastjson:阿里巴巴的
  • jackson:美国FasterXML公司的,Spring框架默认用的

由于以前一直用习惯了阿里的 fastjson,最近突然改为 jackson ,不是太习惯,所以手写一个工具类,应付一下工作中常用的一些方法。

1. 引入依赖包

在 pom.xml 文件中加入以下依赖

<!-- 引入jackson依赖包-->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.14.2</version>
</dependency>

2. 编写 JsonUtil 工具类

package com.yuhuofei.utils;import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;/*** @Description 由于习惯了用fastjson处理JSON数据,突然改成用jackson,有些不适应,所以打算用jackson封装出类似fastjson里的方法进行使用* @ClassName JsonUtil* @Author yuhuofei* @Date 2023/8/19 14:36* @Version 1.0*/
@Slf4j
public class JsonUtil {private static ObjectMapper objectMapper = new ObjectMapper();// 时间日期格式private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";//以静态代码块初始化static {//对象的所有字段全部列入序列化objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);//取消默认转换timestamps形式objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);//忽略空Bean转json的错误objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);//所有的日期格式都统一为以下的格式,即yyyy-MM-dd HH:mm:ssobjectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));//忽略 在json字符串中存在,但在java对象中不存在对应属性的情况。防止错误objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);}/**===========================以下是从JSON中获取对象====================================*/public static <T> T parseObject(String jsonString, Class<T> object) {T t = null;try {t = objectMapper.readValue(jsonString, object);} catch (JsonProcessingException e) {log.error("JsonString转为自定义对象失败:{}", e.getMessage());}return t;}public static <T> T parseObject(File file, Class<T> object) {T t = null;try {t = objectMapper.readValue(file, object);} catch (IOException e) {log.error("从文件中读取json字符串转为自定义对象失败:{}", e.getMessage());}return t;}//将json数组字符串转为指定对象List列表或者Map集合public static <T> T parseJSONArray(String jsonArray, TypeReference<T> reference) {T t = null;try {t = objectMapper.readValue(jsonArray, reference);} catch (JsonProcessingException e) {log.error("JSONArray转为List列表或者Map集合失败:{}", e.getMessage());}return t;}/**=================================以下是将对象转为JSON=====================================*/public static String toJSONString(Object object) {String jsonString = null;try {jsonString = objectMapper.writeValueAsString(object);} catch (JsonProcessingException e) {log.error("Object转JSONString失败:{}", e.getMessage());}return jsonString;}public static byte[] toByteArray(Object object) {byte[] bytes = null;try {bytes = objectMapper.writeValueAsBytes(object);} catch (JsonProcessingException e) {log.error("Object转ByteArray失败:{}", e.getMessage());}return bytes;}public static void objectToFile(File file, Object object) {try {objectMapper.writeValue(file, object);} catch (JsonProcessingException e) {log.error("Object写入文件失败:{}", e.getMessage());} catch (IOException e) {e.printStackTrace();}}/**=============================以下是与JsonNode相关的=======================================*///JsonNode和JSONObject一样,都是JSON树形模型,只不过在jackson中,存在的是JsonNodepublic static JsonNode parseJSONObject(String jsonString) {JsonNode jsonNode = null;try {jsonNode = objectMapper.readTree(jsonString);} catch (JsonProcessingException e) {log.error("JSONString转为JsonNode失败:{}", e.getMessage());}return jsonNode;}public static JsonNode parseJSONObject(Object object) {JsonNode jsonNode = objectMapper.valueToTree(object);return jsonNode;}public static String toJSONString(JsonNode jsonNode) {String jsonString = null;try {jsonString = objectMapper.writeValueAsString(jsonNode);} catch (JsonProcessingException e) {log.error("JsonNode转JSONString失败:{}", e.getMessage());}return jsonString;}//JsonNode是一个抽象类,不能实例化,创建JSON树形模型,得用JsonNode的子类ObjectNode,用法和JSONObject大同小异public static ObjectNode newJSONObject() {return objectMapper.createObjectNode();}//创建JSON数组对象,就像JSONArray一样用public static ArrayNode newJSONArray() {return objectMapper.createArrayNode();}/**===========以下是从JsonNode对象中获取key值的方法,个人觉得有点多余,直接用JsonNode自带的取值方法会好点,出于纠结症,还是补充进来了*/public static String getString(JsonNode jsonObject, String key) {String s = jsonObject.get(key).asText();return s;}public static Integer getInteger(JsonNode jsonObject, String key) {Integer i = jsonObject.get(key).asInt();return i;}public static Boolean getBoolean(JsonNode jsonObject, String key) {Boolean bool = jsonObject.get(key).asBoolean();return bool;}public static JsonNode getJSONObject(JsonNode jsonObject, String key) {JsonNode json = jsonObject.get(key);return json;}
}

3. 测试

新建一个 User 类

package com.yuhuofei.entity;import lombok.Data;import java.io.Serializable;/*** @Description* @ClassName User* @Author yuhuofei* @Date 2023/8/19 14:49* @Version 1.0*/
@Data
public class User implements Serializable {private static final long serialVersionUID = 1L;private Integer id;private String name;private String passWord;
}

新建一个测试类

import com.yuhuofei.entity.User;
import com.yuhuofei.utils.JsonUtil;/*** @Description* @ClassName TestJsonUtil* @Author yuhuofei* @Date 2023/8/19 14:58* @Version 1.0*/
public class TestJsonUtil {public static void main(String[] args) {String jsonString = "{\"id\":11,\"name\":\"小明\",\"passWord\":\"123456\"}";User user = JsonUtil.parseObject(jsonString, User.class);System.out.println(user);}
}

执行 main 方法测试,可以看到在控制台正确地输出了结果。
在这里插入图片描述

http://www.hengruixuexiao.com/news/53568.html

相关文章:

  • 廊坊网站排名优化价格seo根据什么具体优化
  • 班级网站建设维护百度知道
  • 公司做网站需要哪些步骤如何免费做网站网页
  • eclipse 网站开发学习长沙百度快速排名
  • 太原便宜做网站的公司哪家好武汉网站设计公司
  • 网站在线客服怎么做优云优客百度推广效果怎么样
  • 万网网站 banner图片不显示上海网络推广联盟
  • 求推荐软件毕设代做靠谱网站武汉seo广告推广
  • 相亲网站绑定微信怎么做seo品牌
  • 网站建设梦幻创意贵阳网站建设公司
  • 孝感网站开发找优搏徐州网页关键词优化
  • 公司找人做网站需要什么上海网站推广排名公司
  • 惠州网站建设制作seo基础教程
  • 丹东做网站哪家好唐老鸭微信营销软件
  • 西安做网站的公司客服seo完整教程视频教程
  • 成成品网站源码有限公司百度商业账号登录
  • 企业商城网站开发自助建站网站
  • 汕头达濠网络优化工具
  • wordpress搬家后变慢百家号seo
  • 什么网站做企业邮箱服务网站备案信息查询
  • 拼团购物网站开发旺道seo工具
  • 邯郸做移动网站价格表东莞seo广告宣传
  • 对做的网站的改进建议广州专门做seo的公司
  • 简述网站建设的主要内容设计个人网站
  • 绥德网站建设设计seo博客推广
  • 深圳网站建设定制平台深圳建站公司
  • 网站网站注册百度软件开放平台
  • 自己做网站卖东西百度网站的优化方案
  • 您备案的网站名称没有关联性百度宣传广告要多少钱
  • 网站建设有什么费用联合早报 即时消息