内容简介:JSON的几种解析方法
JSON是一种轻量级的数据交换格式,和XML一样广泛应用于前端和后台的交互。如何高效和安全的解析JSON是摆在广大 程序员 面前的难题,如果有 工具 包能帮我们做这些事情就好了,目前有Google公司的Gson,阿里巴巴公司的fastJson,以及Jackson,今天我就讲解下如何通过这3个工具去解析JSON字符串,以及如何将 Java 对象转换成JSON字符串。
1.Gson
首先在工程的pom文件中加入依赖
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3.1</version> <scope>compile</scope> </dependency>
这样工程引入了gson-2.3.1.jar
1.1 将BankDto.java对象转为json字符串
Gson gson = new GsonBuilder().create(); BankDto bankDto = new BankDto(); bankDto.setBankId(1L); bankDto.setBankCode("95555"); bankDto.setBankName("CMB"); bankDto.setState("A"); bankDto.setStateDate(new java.util.Date()); System.out.println(gson.toJson(bankDto, BankDto.class));
输出的结果:
{"bankId":1,"bankName":"CMB","state":"A","stateDate":"Jun 17, 2017 9:55:17 PM","bankCode":"95555"}
1.2 将List转为json字符串
Gson gson = new GsonBuilder().create(); List<BankDto> bankList = new ArrayList<BankDto>(); BankDto bankDto = new BankDto(); bankDto.setBankId(1L); bankDto.setBankCode("95555"); bankDto.setBankName("CMB"); bankDto.setState("A"); bankDto.setStateDate(new java.util.Date()); bankList.add(bankDto); bankDto = new BankDto(); bankDto.setBankId(2L); bankDto.setBankCode("95888"); bankDto.setBankName("ABC"); bankDto.setState("A"); bankDto.setStateDate(new java.util.Date()); bankList.add(bankDto); System.out.println(gson.toJson(bankList));
输出结果
[{"bankId":1,"bankName":"CMB","state":"A","stateDate":"Jun 17, 2017 10:07:47 PM","bankCode":"95555"},{"bankId":2,"bankName":"ABC","state":"A","stateDate":"Jun 17, 2017 10:07:47 PM","bankCode":"95888"}]
1.3 对于嵌套的对象转为json字符串
Gson gson = new GsonBuilder().create(); List<BankDto> bankList = new ArrayList<BankDto>(); BankDto bankDto = new BankDto(); bankDto.setBankId(1L); bankDto.setBankCode("95555"); bankDto.setBankName("CMB"); bankDto.setState("A"); bankDto.setStateDate(new java.util.Date()); bankList.add(bankDto); PersonDto person = new PersonDto(); person.setId(123L); person.setName("jack"); bankDto.setPersonDto(person); System.out.println(gson.toJson(bankList));
输出结果
[{"bankId":1,"bankName":"CMB","state":"A","stateDate":"Jun 17, 2017 10:17:23 PM","bankCode":"95555","personDto":{"id":123,"name":"jack"}}]
1.4 如果我们收到了json字符串,如何将其转为java对象呢
String json = "{\"bankId\":1,\"bankName\":\"CMB\",\"state\":\"A\",\"stateDate\":\"Jun 17, 2017 10:17:23 PM\",\"bankCode\":\"95555\",\"personDto\":{\"id\":123,\"name\":\"jack\"}}"; Gson gson = new GsonBuilder().create(); BankDto bank = gson.fromJson(json, BankDto.class); System.out.println(bank.toString());
将得到的对象打印出来,输出结果
bankId:1,bankCode:95555,bankName:CMB,personId:123,personName:jack
1.5 如果收到了json字符串,但又不知道转为什么java对象,可以直接解析json串
String json = "{\"bankId\":1,\"bankName\":\"CMB\",\"state\":\"A\",\"stateDate\":\"Jun 17, 2017 10:17:23 PM\",\"bankCode\":\"95555\",\"personDto\":{\"id\":123,\"name\":\"jack\"}}"; JsonParser jsonParser = new JsonParser(); JsonElement element = jsonParser.parse(json); JsonObject jsonObject = element.getAsJsonObject(); System.out.println(jsonObject.getAsJsonPrimitive("bankName").getAsString()); System.out.println(jsonObject.getAsJsonPrimitive("bankCode").getAsLong());
输出结果:
CMB 95555
2.fastjson
首先在工程的pom文件中加入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> <scope>compile</scope> </dependency>
这样工程就会引入fastjson-1.1.41.jar
2.1 将BankDto转为json字符串
List<BankDto> bankList = new ArrayList<BankDto>(); BankDto bankDto = new BankDto(); bankDto.setBankId(1L); bankDto.setBankCode("95555"); bankDto.setBankName("CMB"); bankDto.setState("A"); bankDto.setStateDate(new Date()); PersonDto person = new PersonDto(); person.setId(123L); person.setName("jack"); bankDto.setPersonDto(person); bankList.add(bankDto); System.out.println(JSON.toJSONString(bankList));
输出结果
[{"bankCode":"95555","bankId":1,"bankName":"CMB","personDto":{"id":123,"name":"jack"},"state":"A","stateDate":1497711670982}]
和 google相比 ,阿里的fastjson将时间直接转为长整型的时间。
2.2 将json字符串转为BankDto对象
需要注意的是,fastjson只能对json字符串中为长整型的时间才能解析,否则会报错
String json = "{\"bankId\":1,\"bankName\":\"CMB\",\"state\":\"A\",\"stateDate\":\"1497712390838\",\"bankCode\":\"95555\",\"personDto\":{\"id\":123,\"name\":\"jack\"}}"; BankDto bank = JSON.parseObject(json, BankDto.class); System.out.println(bank.toString());
输出结果
bankId:1,bankCode:95555,bankName:CMB,stateDate:Sat Jun 17 23:13:10 CST 2017,personId:123,personName:jack
3. Jackson
首先在pom文件中加入下面两个依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.2</version> <scope>compile</scope> </dependency>
jackson-databind 是最主要的,其中就含有重要的对象转换的类ObjectMapper,jackson-annotations 这个是必须要的,否则项目就会报错,另外它含有一个重要的包jackson-core,这个包不需要引用,自动就会通过依赖加载。
3.1 将对象转为json字符串
ObjectMapper objectMapper = new ObjectMapper(); try { System.out.println(objectMapper.writeValueAsString(bankList)); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); }
结果:
[{"bankId":1,"parentId":null,"bankName":"CMB","comments":null,"state":"A","stateDate":1497713764810,"directDebitFlag":null,"bankCode":"95555","spId":null,"personDto":{"id":123,"name":"jack"}}]
3.2 将字符串转为对象
ObjectMapper objectMapper = new ObjectMapper(); try { BankDto bank = objectMapper.readValue(json, BankDto.class); System.out.println(bank.toString()); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
结果:
bankId:1,bankCode:95555,bankName:CMB,stateDate:Sat Jun 17 23:13:10 CST 2017,personId:123,personName:jack
总结:
总共讲解了三家公司的三个jar包实现对JSON字符串的解析和转化,Google公司的Gson个人认为更胜一筹,其在转化的时候对数据类型保持的比较好,fastjson和Jackson则在处理时间上会进行转换。大家如果要学习关于这些包是如何对JSON进行解析和转化的,个人建议可以通过去看Jackson的源码比较好,因为它提供的API都是很原生的,大家可以从它的方法上就可以看出,另外两个都包装得很厉害。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 小程序封装路由文件和路由方法,5种路由方法全解析
- 无符号Golang程序逆向方法解析
- PHP几种常见魔术方法解析
- Reface.NPI 方法名称解析规则详解
- Unity引擎iOS增量编译方法解析
- SpringBoot源码解析之main方法推断
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Rationality for Mortals
Gerd Gigerenzer / Oxford University Press, USA / 2008-05-02 / USD 65.00
Gerd Gigerenzer's influential work examines the rationality of individuals not from the perspective of logic or probability, but from the point of view of adaptation to the real world of human behavio......一起来看看 《Rationality for Mortals》 这本书的介绍吧!