解析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的更多相关文章

  1. 廖雪峰Java14Java操作XML和JSON-2JSON-1Json介绍

    JSON是一种类似JavaScript对象的数据表示格式 JavaScript Object Notation 去除了JavaScript的执行语句 仅保留数据 JSON格式: 仅保留UTF-8编码 ...

  2. 廖雪峰Java14Java操作XML和JSON-1XML-4第三方XML库

    总结: 使用Jackson可以快速在XML和JavaBean之间互相转换 可使用Annotation定制序列化和反序列化

  3. 廖雪峰Java14Java操作XML和JSON-1XML-3SAX

    SAX:Simple API for XML 基于事件的API import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPars ...

  4. 廖雪峰Java14Java操作XML和JSON-1XML-2DOM

    XML是一种数据表示形式. 可以描述非常复杂的数据数据结构 用于传输和传输数据 DOM:Document Object Model DOM模型就是把XML文档作为一个树形结构,从根结点开始,每个节点都 ...

  5. 廖雪峰Java14Java操作XML和JSON-1XML-1XML介绍

    1.XML:可扩展标记语言(extensible Markup Language) 是一种数据表示格式 可以描述非常复杂的数据结构 用于存储和传输数据 1.1XML特点: 1.纯文本,默认utf-8编 ...

  6. 廖雪峰js教程笔记9 json

    JSON是JavaScript Object Notation的缩写,它是一种数据交换格式. 在JSON出现之前,大家一直用XML来传递数据.因为XML是一种纯文本格式,所以它适合在网络上交换数据.X ...

  7. 转 廖雪峰 urllib

    http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432688314740a0 ...

  8. XMl入门介绍及php操作XML

    一.什么是XML XML全称:Extensible Markup Language 中文名:可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标 ...

  9. 爬虫:把廖雪峰的教程转换成 PDF 电子书

    写爬虫似乎没有比用 Python 更合适了,Python 社区提供的爬虫工具多得让你眼花缭乱,各种拿来就可以直接用的 library 分分钟就可以写出一个爬虫出来,今天就琢磨着写一个爬虫,将廖雪峰的 ...

随机推荐

  1. 2019 牛客多校第六场 J Upgrading Technology

    题目链接:https://ac.nowcoder.com/acm/contest/886/J 题目大意 略. 分析 见代码. 代码如下 #include <bits/stdc++.h> u ...

  2. 剑指offer——02二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  3. C++数据类型之实型(浮点型)&科学计数法

    实型(浮点型) **作用**:用于==表示小数== 浮点型变量分为两种: 1. 单精度float 2. 双精度double 两者的**区别**在于表示的有效数字范围不同. float类型数据,需在数据 ...

  4. luogu3384 /// 树链剖分+线段树模板

    题目大意: https://www.luogu.org/problemnew/show/P3384 树链剖分的讲解 两个dfs() 修改 查询 很详细很好理解 https://www.cnblogs. ...

  5. Codeforces 479【A】div3试个水

    题目链接:http://codeforces.com/problemset/problem/977/A 题意:这个题,题目就是让你根据他的规律玩嘛.末尾是0就除10,不是就-1. 题解:题解即题意. ...

  6. iOS开发系列-NSDate

    NSDate API 获取当前时间 获取时间戳 创建间隔指定时间戳的Date // 获取昨天 NSTimeInterval time = 24 * 60 * 60; NSDate *date = [N ...

  7. pycharm连接数据库及相应操作

    1.连接数据库 2.pycharm中数据库的操作

  8. cvErode和cvDilate腐蚀和膨胀函数

    Erode腐蚀,Dilate膨胀,这两个形态学函数总是成对出现,前者可以消除较小独点如噪音,后者可以使不连通的图像合并成块. void cvErode( const CvArr* src, CvArr ...

  9. delphi 第4课

    try 语句;(正常)except 语句; (意外处理部分) end: 例子: begin sum:=; try n:=strtoint(edit1.Text); except showMessage ...

  10. MySQL Download

    { http://www.wampserver.com/#wampserver-64-bits-php-5-6-25-php-7 }