1.  什么是 Json 序列化 和 反序列化

序列化 => 将 Java对象 转换成 json字符串
反序列化 => 将 json字符串 转换成 Java对象

2. 依赖包 说明

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
依赖包说明:
# Annotation support
Maven: com.fasterxml.jackson.core:jackson-annotations:2.7.0
# The core function support
Maven: com.fasterxml.jackson.core:jackson-core:2.10.1
Maven: com.fasterxml.jackson.core:jackson-databind:2.7.4

3. ObjectMapper

    功能:
ObjectMapper 提供了 读写Json的功能
读:
public <T> T readValue(String content, Class<T> valueType)
将 字符串content 反序列化 成 Class<T>类型的对象 写(可以把JSON字符串保存File、OutputStream等不同的介质中):
public void writeValue(File resultFile, Object value)
将 Objec转成 json字符串,并保存到 指定 File 内 public void writeValue(OutputStream out, Object value)
将 Objec转成 json字符串,并保存到 指定 OutputStream 内(使用 UTF8 编码) public byte[] writeValueAsBytes(Object value)
将 Objec转成 json字符串,And output the results 字节数组 public String writeValueAsString(Object value)
将 Objec转成 json字符串,And output the results 字符串

4. Json 注解

    @JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性

    @JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss").
示例: @JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss") @JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称
示例: @JsonProperty("AliasName")

5. Bean对象 => Json 字符串 (Json 序列化)

// TODO Used to convert Bean
case class UserBean(@BeanProperty name: String
, @BeanProperty age: Int
, @BeanProperty birthday: Date
, @BeanProperty email: String) // TODO Bean对象 => Json 字符串 (Json 序列化)
object Bean2Json extends App { val user: UserBean = UserBean("张飞", 21, new Date(), "[email protected]") val mapper: ObjectMapper = new ObjectMapper() // user 对象 转 json字符串
val beanJson: String = mapper.writeValueAsString(user) println(beanJson)
// {"name":"张飞","age":21,"birthday":1660115099317,"email":"[email protected]"} // scala 集合 转 json字符串
val list: Array[UserBean] = Array(
UserBean("刘备", 21, new Date(), "[email protected]"),
UserBean("关羽", 22, new Date(), "[email protected]"),
UserBean("张飞", 23, new Date(), "[email protected]")
) private val listJson: String = mapper.writeValueAsString(list) println(listJson)
// [{"name":"刘备","age":21,"birthday":1660115099712,"email":"[email protected]"},{"name":"关羽","age":22,"birthday":1660115099712,"email":"[email protected]"},{"name":"张飞","age":23,"birthday":1660115099712,"email":"[email protected]"}]
}

6. Json 字符串 => Bean对象 (Json 反序列化)

// TODO 由于 利用反射 对json串反序列化,所有必须有 空参构造 + get、set方法
class UserBean1() {
@BeanProperty var name: String = _
@BeanProperty var age: Int = _
@BeanProperty var birthday: Date = _
@BeanProperty var email: String = _ override def toString = s"UserBean1($name, $age, $birthday, $email)"
} // TODO Json 字符串 => Bean对象 (Json 反序列化)
object Json2Bean extends App {
val jsonBean: String =
"""
|{"name":"张飞","age":21,"birthday":1660115099317,"email":"[email protected]"}
""".stripMargin val jsonArray: String =
"""
|[{"name":"刘备","age":21,"birthday":1660115099712,"email":"[email protected]"},{"name":"关羽","age":22,"birthday":1660115099712,"email":"[email protected]"},{"name":"张飞","age":23,"birthday":1660115099712,"email":"[email protected]"}]
""".stripMargin val mapper: ObjectMapper = new ObjectMapper() // json 转 bean 对象
private val bean: UserBean1 = mapper.readValue(jsonBean, classOf[UserBean1]) // json 数组 转 bean 对象数组
private val array: Array[UserBean1] = mapper.readValue(jsonArray, classOf[Array[UserBean1]]) array.foreach(println(_)) println(bean)
}

7. 使用注解

class UserBean2() {
@JsonIgnore //进行JSON操作时忽略该属性
@BeanProperty var name: String = _ @BeanProperty var age: Int = _ @JsonFormat(pattern = "yyyy-MM-dd") // 格式化日期属性
@BeanProperty var birthday: Date = _ @JsonProperty("mail_Alias")
@BeanProperty var email: String = _ override def toString = s"UserBean2($name, $age, $birthday, $email)"
} // TODO 使用注解
object Bean2Json_Anno extends App { val user: UserBean2 = new UserBean2() user.setAge(21)
user.setName("张飞")
user.setBirthday(new Date())
user.setEmail("[email protected]") val mapper: ObjectMapper = new ObjectMapper() // user 对象 转 json字符串
val beanJson: String = mapper.writeValueAsString(user) println(beanJson)
// {"age":21,"birthday":"2022-08-10","mail_Alias":"[email protected]"} }

Scala中使用 Jackson API 进行JSONSerialization and deserialization of more related articles

  1. Net中JSON序列化和反序列化处理(日期时间特殊处理)

    0  缘由 笔者最近在web api端使用Json.Net进行序列化处理,而在调用端使用DataContractSerializer进行反序列化,遇到日期时间处理反序列化不成功[备注:笔者使用Net ...

  2. WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    WPF中的常用布局   一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...

  3. Java下用Jackson进行JSON序列化和反序列化(转)

    Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的J ...

  4. C#中JSON序列化和反序列化

    有一段时间没有到博客园写技术博客了,不过每天逛逛博客园中大牛的博客还是有的,学无止境…… 最近在写些调用他人接口的程序,用到了大量的JSON.XML序列化和反序列化,今天就来总结下json的序列化和反 ...

  5. 在net中json序列化与反序列化

    准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则: 数据在键值对中 数据由逗号分隔 花括 ...

  6. 在SpringMVC中,当Json序列化,反序列化失败的时候,会抛出HttpMessageNotReadableException异常, 当Bean validation失败的时候,会抛出MethodArgumentNotValidException异常,因此,只需要在ExceptionHandler类中添加处理对应异常的方法即可.

    在SpringMVC中,当Json序列化,反序列化失败的时候,会抛出HttpMessageNotReadableException异常, 当Bean validation失败的时候,会抛出Method ...

  7. 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变

    在net中json序列化与反序列化   准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...

  8. JasksonThe first:7篇-Class inheritance underJSON序列化与反序列化JsonTypeInfo

    Jackson是Spring Boot(SpringBoot)默认的JSON数据处理框架,但是其并不依赖于任何的Spring 库.有的小伙伴以为Jackson只能在Spring框架内使用,其实不是的, ...

  9. C# JSON 序列化和反序列化——JavaScriptSerializer实现

    一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...

  10. 几种常用的jsonSerialization and deserialization tool is introduced

    一.前言 JsonOften used in serialization and deserialization work,Is the current data interaction common format,RestStyle interface andjson格式的数据交互,真的是天作之合. 目前Json字符与JsonThe object's transformation way has a lot of,接下 ...

随机推荐

  1. Dozer应用——Value mapping between classes

    1. Mappings via Annotation public class SourceBean { private Long id; private String name; @Mapping( ...

  2. 我们为什么要用springcloud?

    1 2 单体架构 在网站开发的前期,项目面临的流量相对较少,单一应用可以实现我们所需要的功能,从而减少开发.部署和维护的难度.这种用于简单的增删改查的数据访问框架(ORM)十分的重要.  垂直应用架构 ...

  3. 2019年3月8日_CCF-AArticles to share

    3月8日下午3点开始,Teacher in charge of Wang Lipeng lab meeting,Laboratory personnel to participate in the.First Wang Lipeng teacher base problems existing in the whole book once again, the instruction and how to modify part, 其次,Wang Lipeng teacher in recent period of time to do the related work summary and stage task points ...

  4. LeetCode-两个结构分别遍历,然后合并

    今天做了leetcode67题,两个2进制数相加,回想了一下其实有很多这种类型的题,比如leetcode2两数相加. 在做这种题时我自己的思路就是先循环遍历一个短的,然后跳出循环,判断是哪个结束,再接 ...

  5. kmp匹配详解

    String algorithm is cancer 一.kmpThe usefulness of algorithm In a text string to find the location of the pattern string,数量 文本串:To look for pattern string in this string 模式串:In text strings of string 全是废话 二.kmp算法的思想 话说kmp好像是3个发明 ...

  6. jspThe page more“接收数据”与“Page cycle data”是否相等

    Relational operators in the page: -lt 小于 -le   小于或者等于 -gt 大于 -ge 大于或者等于 -eq 等于 -ne 不等于 判空:<c:if test="${empty count  ...

  7. IE9以下不支持placeholder属性

    jquery.placeholder.min.js 这个jsGo to a website searchhttps://www.bootcdn.cn/jquery-placeholder/ 使用这个jquery.placeho ...

  8. How do I use screen on the Linux systems?

    Scope The screen utility provides a way to run a command on a Linux system, detach from it, and then ...

  9. CentOS6安装NodeJS(非编译)

    Because the compiler installed need various dependent libraries,Will be much higher than the default version of the production environment,Forced upgrade will produce a lot of unnecessary problems,So generally use website compiled installation 下载nodejs并安装 wget https://nodejs.org/dist ...

  10. JavaScript toString() 方法

    注意:在JavaScript中,数字后面的"."Operator: yes significance is not sure.Because it can be a sign of a floating point number,May be will take an object attribute operator.但是JavaScriptThe interpreter as him ...