廖雪峰Java14Java操作XML和JSON-2JSON-2处理JSON
解析JSON
- JSR 353 API
- 常用的第三方库
* Jackson
* gson
* fastjson
Jackson:
- 提供了读写JSON的API
- JSON和JavaBean可以互相转换
- 可食用Annotation定制序列化和反序列化
Jackson初步使用
依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.0</version>
</dependency>
package com.feiyangedu.sample.pop3;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ParseJsonTest {
public static void main(String[] args) {
String data = "{\"type\":2,\"range\":1,\"start\":1368417600,\"end\":1368547140,"
+ "\"cityName\":\"天津\",\"companyIds\":[\"12000001\"],\"companyNames\":[\"天津\"],"
+ "\"12000001\":{\"data\":[47947,48328,48573,48520],"
+ "\"timestamps\":[1368417600,1368417900,1368418200,1368418500]}}";
String data2 = parseJson(data);
System.out.println(data2);
}
public static String parseJson(String data){
StringBuffer buffer = new StringBuffer();
try{
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(data); //读取JSON
//根据key获取对应的value
int type = rootNode.path("type").asInt();
int range = rootNode.path("range").asInt();
long start = rootNode.path("start").asLong();
long end = rootNode.path("end").asLong();
String cityName = rootNode.path("cityName").asText();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String str = "类型(type):"+type+"\r\n"+"范围(range):"+range+"\r\n"+
"开始时间(start):"+sdf.format(new Date(start*1000))+"\r\n"+"结束时间(end):"+sdf.format(new Date(end*1000))+"\r\n"+
sdf.format(new Date(end*1000))+"\r\n"+"城市名称(cityName):"+cityName;
buffer.append(str);
JsonNode companyIds = rootNode.path("companyIds");
JsonNode companyNames = rootNode.path("companyNames");
for(int i=0;i<companyIds.size();i++){
String companyId = companyIds.get(i).asText();
String companyName = companyNames.get(i).asText();
JsonNode infoNode = rootNode.path(companyId);
JsonNode dataNode = infoNode.path("data");
JsonNode timestampsNode = infoNode.path("timestamps");
buffer.append("\r\n{\r\n 公司ID(companyId):"+companyId+"\r\n 公司名称(companyName):"+companyName+"\r\n"+"data:");
for(int j=0;j<dataNode.size();j++){
long dataValue = dataNode.get(j).asLong();
buffer.append(dataValue+",");
}
buffer.append("\r\n timestamp:");
for(int k=0;k<timestampsNode.size();k++){
long timeValue = timestampsNode.get(k).asLong();
buffer.append(sdf.format(new Date(timeValue*1000))+",");
}
buffer.append("\r\n}\r\n");
}
}catch (JsonProcessingException e){
e.printStackTrace();;
}catch (IOException e){
e.printStackTrace();
}
return buffer.toString();
}
}
使用JavaBean读取JSON
JsonToBean.java
package csj2019;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class JsonToBean {
//来自豆瓣的API,传入一个关键字,返回符合条件的书籍,结果为JSON格式的字符串
static final String JSON_URL = "https://api.douban.com/v2/book/search?apikey=0df993c66c0c636e29ecbb5344252a4a&q=";
//如何把JSON解析为Java对象
public static void main(String[] args) throws Exception {
//创建ObjectMapper对象,对mapper设置一些值,通过registerModule注册一个JavaTimeModule,具有序列化和反序列化Java8日期时间的格式
ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
//配置DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES为false,自动忽略JavaBean中找不到的属性
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//返回JSON字符串
String json = search("java");
//通过readValue把一个JSON字符串反序列化成指定的Java对象,这里的Java对象是SearchResult这个类
SearchResult result = mapper.readValue(json, SearchResult.class);
//用mapper对象把JavaBean序列化为JSON并且输出
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result));
}
static String search(String q) {
HttpURLConnection conn = null;
StringBuilder sb = new StringBuilder(4096);
try {
URL url = new URL(JSON_URL + URLEncoder.encode(q, "UTF-8"));
conn = (HttpURLConnection) url.openConnection();
if (200 == conn.getResponseCode()) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"))) {
char[] buffer = new char[1024];
for (;;) {
int n = reader.read(buffer);
if (n == (-1)) {
break;
}
sb.append(buffer, 0, n);
}
}
return sb.toString();
}
throw new RuntimeException("Bad response code: " + conn.getResponseCode());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
}
SearchResult.java
import java.util.List;
public class SearchResult {
//用SearchResult来表示整个JSON对象,包含count,total,books数组,Book在Book类中
public long count;
public long total;
public List<Book> books;
}
Book.java
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.time.LocalDate;
import java.util.List;
public class Book {
public String id;
public String title;
public String subtitle;
public List<String> author;
@JsonSerialize(using = CustomLocalDateSerializer.class) //序列化使用自定义的指定类
@JsonDeserialize(using = CustomLocalDateDeserializer.class) //反序列化使用自定义的指定类
public LocalDate pubdate;
public String url;
public String price;
}
CustomLocalDateDeserializer.java
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class CustomLocalDateDeserializer extends JsonDeserializer<LocalDate> {
static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-M-d",Locale.US);
@Override
//覆写deserialize
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,JsonProcessingException {
//获得原始的String
String s = p.getValueAsString();
if(s!=null){
try{
//用自己的格式来解析LocalDate对象
return LocalDate.parse(s,FORMATTER);
}catch (DateTimeException e){
//ignore
}
}
return null;
}
}
CustomLocalDateSerializer.java
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;
import java.time.LocalDate;
public class CustomLocalDateSerializer extends JsonSerializer<LocalDate> {
//把自定义的LocalDate对象序列化为一个String对象
@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.toString());
}
}
总结:
Jackson提供了读写JSON的API
- 实现JSON和JavaBean的互相转换
- 可使用Annotation定制序列化和反序列化
City.java
public class City {
private String id;
private String cityName;
public City(){};
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
public void setCityName(String cityName){
this.cityName = cityName;
}
public String getCityName(){
return cityName;
}
}
Province.java
import java.util.Date;
import java.util.List;
public class Province {
private Integer id;
private String name;
private Date birthDate;
private List<City> cites;
public Province(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public List<City> getCites() {
return cites;
}
public void setCites(List<City> cites) {
this.cites = cites;
}
}
Country.java
import java.util.*;
public class Country {
private Integer id;
private String countryName;
private Date establishTime;
private List<Province> provinces;
private String[] lakes;
private Map<String,String> forest = new HashMap<String,String>();
public Country(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public Date getEstablishTime() {
return establishTime;
}
public void setEstablishTime(Date establishTime) {
this.establishTime = establishTime;
}
public List<Province> getProvinces() {
return provinces;
}
public void setProvinces(List<Province> provinces) {
this.provinces = provinces;
}
public String[] getLakes() {
return lakes;
}
public void setLakes(String[] lakes) {
this.lakes = lakes;
}
public Map<String, String> getForest() {
return forest;
}
public void setForest(Map<String, String> forest) {
this.forest = forest;
}
}
Bean2JsonStr
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class Bean2JsonStr {
public static void main(String[] args) throws ParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
mapper.setDateFormat(sdf);
City city1 = new City();
city1.setCityName("杭州");
city1.setId("0571");
City city2 = new City();
city2.setCityName("宁波");
city2.setId("0572");
Province province = new Province();
province.setBirthDate(new Date());
List<City> cities = new ArrayList<City>();
cities.add(city1);
cities.add(city2);
province.setCites(cities);
// province.setId(1);
province.setName("浙江");
Country country = new Country();
country.setCountryName("中国");
country.setId(1);
country.setEstablishTime(sdf.parse("1949-10-01"));
country.setLakes(new String[]{"青海湖","微山湖","洞庭湖","太湖","鄱阳湖"});
HashMap<String,String> forests = new HashMap<String,String>();
forests.put("NO.1","大兴安岭");
forests.put("NO.2","小兴安岭");
country.setForest(forests);
List<Province> provinces = new ArrayList<Province>();
provinces.add(province);
country.setProvinces(provinces);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.writeValue(new File("county.json"),country);
}
}
Jsonstr2Bean.java
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
public class Jsonstr2Bean {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
File jsonFile = new File("county.json");
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Country country = mapper.readValue(jsonFile,Country.class);
System.out.println(country.getCountryName()+country.getEstablishTime());
List<Province> provinces = country.getProvinces();
for(Province province:provinces){
System.out.println(province.getName()+"\t"+province.getBirthDate());
for(City city:province.getCites()){
System.out.println(city.getId()+"\t"+city.getCityName());
}
}
}
}
Jsonstr2List.java
```#java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Jsonstr2List {
public static void main(String[] args) throws JsonProcessingException,IOException {
City city1 = new City();
city1.setId("0571");
city1.setCityName("杭州");
City city2 = new City();
city2.setId("0572");
city2.setCityName("宁波");
List<City> cites = new ArrayList<City>();
cites.add(city1);
cites.add(city2);
ObjectMapper mapper = new ObjectMapper();
String listJsonStr = mapper.writeValueAsString(cites);
System.out.println(listJsonStr);
List<City> list = mapper.readValue(listJsonStr,new TypeReference<List<City>>(){});
for(City city:list){
System.out.println(city.getId()+"\t"+city.getCityName());
}
}
}
<img src="https://img2018.cnblogs.com/blog/1418970/201908/1418970-20190828201936155-1300012672.png" width="500" />
参考:
[https://www.cnblogs.com/williamjie/p/9242932.html](https://www.cnblogs.com/williamjie/p/9242932.html)
[https://www.cnblogs.com/williamjie/p/9242451.html](https://www.cnblogs.com/williamjie/p/9242451.html)
廖雪峰Java14Java操作XML和JSON-2JSON-2处理JSON的更多相关文章
- 廖雪峰Java14Java操作XML和JSON-2JSON-1Json介绍
JSON是一种类似JavaScript对象的数据表示格式 JavaScript Object Notation 去除了JavaScript的执行语句 仅保留数据 JSON格式: 仅保留UTF-8编码 ...
- 廖雪峰Java14Java操作XML和JSON-1XML-4第三方XML库
总结: 使用Jackson可以快速在XML和JavaBean之间互相转换 可使用Annotation定制序列化和反序列化
- 廖雪峰Java14Java操作XML和JSON-1XML-3SAX
SAX:Simple API for XML 基于事件的API import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPars ...
- 廖雪峰Java14Java操作XML和JSON-1XML-2DOM
XML是一种数据表示形式. 可以描述非常复杂的数据数据结构 用于传输和传输数据 DOM:Document Object Model DOM模型就是把XML文档作为一个树形结构,从根结点开始,每个节点都 ...
- 廖雪峰Java14Java操作XML和JSON-1XML-1XML介绍
1.XML:可扩展标记语言(extensible Markup Language) 是一种数据表示格式 可以描述非常复杂的数据结构 用于存储和传输数据 1.1XML特点: 1.纯文本,默认utf-8编 ...
- 廖雪峰js教程笔记9 json
JSON是JavaScript Object Notation的缩写,它是一种数据交换格式. 在JSON出现之前,大家一直用XML来传递数据.因为XML是一种纯文本格式,所以它适合在网络上交换数据.X ...
- 转 廖雪峰 urllib
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432688314740a0 ...
- XMl入门介绍及php操作XML
一.什么是XML XML全称:Extensible Markup Language 中文名:可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标 ...
- 爬虫:把廖雪峰的教程转换成 PDF 电子书
写爬虫似乎没有比用 Python 更合适了,Python 社区提供的爬虫工具多得让你眼花缭乱,各种拿来就可以直接用的 library 分分钟就可以写出一个爬虫出来,今天就琢磨着写一个爬虫,将廖雪峰的 ...
随机推荐
- CSS3:CSS3 简介
ylbtech-CSS3:CSS3 简介 1.返回顶部 1. CSS3 简介 对CSS3已完全向后兼容,所以你就不必改变现有的设计.浏览器将永远支持CSS2. CSS3 模块 CSS3被拆分为&quo ...
- C89,C99: C数组&结构体&联合体快速初始化
1. 背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组 ...
- DNS隧道--dnscat2
安装 服务端 git clone https://github.com/iagox86/dnscat2.git cd dnscat2 cd server sudo gem install bundle ...
- <router-link :to="...">
一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串<router-link to=& ...
- Python+Django+SAE系列教程6-----本地配置Django
前五章.我们介绍了Python的语法,本章開始介绍Django. Python的Web框架有非常多,有Django.web2py.tornado.web.py等.我们这里选 则Django.至于这些框 ...
- js正则表达式常见面试题
1 . 给一个连字符串例如:get-element-by-id转化成驼峰形式. var str = "get-element-by-id"; var reg = /-\w/g; / ...
- SQL语句:一张表和另一张表的多重匹配查询
1.两个表结构如下图 2.如何查询成如下图所示 3.SQL语句是: select id,name=stuff(( select ','+t2.name from a t1 join b t2 on c ...
- Jquery中attr与prop的区别详解
prop()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性,返回值是空字符串. attr()函数的结果: 1.如果有相应的属性,返回指定属性值. 2.如果没有相应的属性, ...
- poj1363 Rails Central Europe 1997
P.S.: 输出换行 三个方法 1.直接按照要求做 根据给的数,需要push,pop哪些数据,具有唯一性 数最多进栈一次,出栈一次 O(n) Source Code Problem: User: co ...
- css样式初始化代码总结
编写css样式之前需要初始化css样式,总结如下: /* CSS Document */ html, body, div, span, object, iframe,h1, h2, h3, h4, h ...