json 和对象互相转换
json 和对象互相转换
导入 Jar 包:
import com.fasterxml.jackson.databind.ObjectMapper;
Maven 地址:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
private static ObjectMapper objectMapper = new ObjectMapper();
把对象转换成 json 串
public static String toStr(Object obj) {
String json_str = "";
try {
json_str = objectMapper.writer().writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return json_str;
}
把 json 串转换成对象
public static <T> T toObject(String jsonStr, Class<T> valueType) {
try {
return objectMapper.readValue(jsonStr, valueType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
完整代码...
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
public static String toStr(Object obj) {
String json_str = "";
try {
json_str = objectMapper.writer().writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return json_str;
}
public static <T> T toObject(String jsonStr, Class<T> valueType) {
try {
return objectMapper.readValue(jsonStr, valueType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String createOperaStr(boolean b, String msg) {
return "{\"success\":" + b + ",\"msgText\":\"" + msg + "\"}";
}
}
</div>
json 和对象互相转换的更多相关文章
- JSON转换类(二)--List转换成Json、对象集合转换Json等
#region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...
- List转换成Json、对象集合转换Json等
#region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...
- js与json的区别,json的概述,json与面向对象,json与对象的转换
<script> //js与json的区别,json的概述,json与面向对象,json与对象的转换 //json的概述:json(javascript object Notation,j ...
- scala中json与对象的转换
遇到的问题 因为要把spark从es读出来的json数据转换为对象,开始想用case class定义类型,通过fastjson做转换.如下 case class Book (author: Strin ...
- js json和对象互相转换
http://www.jb51.net/article/44562.htm obj = JSON.parse(string) | obj = jQuery.parseJSON(str) 将JSON字符 ...
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
- Newtonsoft.Json 把对象转换成json字符串
var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...
- json 数组 对象 xml 之间转换(待补充)
json 数组 xml 对象 之间转换(待补充) 1 把对象的类型或者数组转换成字符串类型(或者更确切的说是json类型的). 此处参考链接http://www.jb51.net/article ...
- python爬虫requests json与字典对象互相转换
import requests import json ''' json.loads(json_str) json字符串转换成字典 json.dumps(dict) 字典转换成json字符串 ''' ...
随机推荐
- 【概率论】5-3:超几何分布(The Hypergeomtric Distribution)
title: [概率论]5-3:超几何分布(The Hypergeomtric Distribution) categories: - Mathematic - Probability keyword ...
- 【概率论】5-1:分布介绍(Special Distribution Introduction)
title: [概率论]5-1:分布介绍(Special Distribution Introduction) categories: - Mathematic - Probability keywo ...
- ROSservice 通信方式
操作演示,对 service 通信的理解请看:点击打开链接 1. 使用 rosservice 1.1 rosservice list 假设小乌龟节点仍在运行 rosrun turtlesim tu ...
- Allure自动化测试报告之修改allure测试报告logo
1.安装allure 2.进入 /usr/local/Cellar/allure/2.10.0/libexec/config 3.在allure.yml添加 - custom-logo-plugin ...
- rabbitmq 删除所有队列及服务重启脚本
#!/bin/bash # 删除元数据 rm -rf /var/lib/rabbitmq/mnesia # 重启rabbitmq rabbitmqctl stop systemctl restart ...
- hadoop(10)---hdfs配置文件详解
以下只是简单的对hdfs(hdfs.site.xml)配置文件做一个简单的说明. <configuration><property><!-- 为namenode集群定义一 ...
- [Shell]CVE-2019-0708漏洞复现及修复补丁
0x01 漏洞原理 Windows系列服务器于2019年5月15号,被爆出高危漏洞,该漏洞影响范围较广,windows2003.windows2008.windows2008 R2.windows 7 ...
- 上下文对象-请求对象-响应对象(ServletContext - response - request)
目录 上下文对象-请求对象-响应对象 ServletContext response request 上下文对象-请求对象-响应对象 ServletContext 什么是ServletContext ...
- js学习笔记(1)
前言 因为后期的软件工程组队作业需要js,在纪华裕大佬的带领下(我觉得他好像更喜欢纪华裕这个名字),我开始了js的学习.其实这篇博客应该在两天前的晚上就发出来了,因为忙着写个人编程,拖到了现在,组 ...
- 配置 Ubuntu 服务器
Python: apt install python3-pip sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update ap ...