Java--Json解析
普通Json
{"code":"S0000", "describe":"数据正常返回", "result":"OK"}
package gsondemo; import java.io.Serializable;
import java.util.List; @SuppressWarnings("serial")
public class Common implements Serializable { private String code;
private String describe;
private String result; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getDescribe() {
return describe;
} public void setDescribe(String describe) {
this.describe = describe;
} public String getResult() {
return result;
} public void setResult(String result) {
this.result = result;
} @Override
public String toString() {
return "Common [code=" + code + ", describe=" + describe + ", result=" + result + "]";
} }
package gsondemo; import com.google.gson.Gson; public class CommonAna { public static void main(String[] args) {
String json = "{\"code\":\"S0000\", \"describe\":\"数据正常返回\", \"result\":\"OK\"}";
Gson gson = new Gson();
Common common = gson.fromJson(json, Common.class);
System.out.println(common);
} }
嵌套json对象
{"code":"S0000",
"describe":"数据正常返回",
"result":{"accountBlank":"xxxxxx",
"bankAccount":"xxxxxx",
"code":"xxx",
"id":"xxxxxxx",
"kpAddr":"xxxxxx",
"kpCode":"xxxxx",
"kpName":"xxxxx",
"kpTel":"xxx"}}
package gsondemo; import java.io.Serializable; @SuppressWarnings("serial")
public class Sub implements Serializable { private String accountBlank;
private String bankAccount;
private String code;
private String id;
private String kpAddr;
private String kpCode;
private String kpName;
private String kpTel; public String getAccountBlank() {
return accountBlank;
} public void setAccountBlank(String accountBlank) {
this.accountBlank = accountBlank;
} public String getBankAccount() {
return bankAccount;
} public void setBankAccount(String bankAccount) {
this.bankAccount = bankAccount;
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getKpAddr() {
return kpAddr;
} public void setKpAddr(String kpAddr) {
this.kpAddr = kpAddr;
} public String getKpCode() {
return kpCode;
} public void setKpCode(String kpCode) {
this.kpCode = kpCode;
} public String getKpName() {
return kpName;
} public void setKpName(String kpName) {
this.kpName = kpName;
} public String getKpTel() {
return kpTel;
} public void setKpTel(String kpTel) {
this.kpTel = kpTel;
} @Override
public String toString() {
return "NuonuoBean_Result [accountBlank=" + accountBlank + ", bankAccount=" + bankAccount + ", code=" + code
+ ", id=" + id + ", kpAddr=" + kpAddr + ", kpCode=" + kpCode + ", kpName=" + kpName + ", kpTel=" + kpTel
+ "]";
} }
package gsondemo; import java.io.Serializable;
import java.util.List; @SuppressWarnings("serial")
public class CommonWithObject implements Serializable { private String code;
private String describe;
private Sub result; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getDescribe() {
return describe;
} public void setDescribe(String describe) {
this.describe = describe;
} public Sub getResult() {
return result;
} public void setResult(Sub result) {
this.result = result;
} @Override
public String toString() {
return "CommonWithObject [code=" + code + ", describe=" + describe + ", result=" + result + "]";
} }
package gsondemo; import com.google.gson.Gson; public class WithObjectAna { public static void main(String[] args) {
String json = "{\"code\":\"S0000\","
+" \"describe\":\"数据正常返回\","
+" \"result\":{\"accountBlank\":\"xxxxxx\","
+" \"bankAccount\":\"xxxxxx\","
+" \"code\":\"xxx\","
+" \"id\":\"xxxxxxx\","
+" \"kpAddr\":\"xxxxxx\","
+" \"kpCode\":\"xxxxx\","
+" \"kpName\":\"xxxxx\","
+" \"kpTel\":\"xxx\"}}";
Gson gson = new Gson();
CommonWithObject commonWithObject = gson.fromJson(json, CommonWithObject.class);
System.out.println(commonWithObject);
} }
嵌套json数组
{"code":"S0000",
"describe":"数据正常返回",
"result":[{"accountBlank":"xxxxxx",
"bankAccount":"xxxxxx",
"code":"xxx",
"id":"xxxxxxx",
"kpAddr":"xxxxxx",
"kpCode":"xxxxx",
"kpName":"xxxxx",
"kpTel":"xxx"}]}
package gsondemo; import java.io.Serializable;
import java.util.List; @SuppressWarnings("serial")
public class CommonWithArray implements Serializable { private String code;
private String describe;
private List<Sub> result; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getDescribe() {
return describe;
} public void setDescribe(String describe) {
this.describe = describe;
} public List<Sub> getResult() {
return result;
} public void setResult(List<Sub> result) {
this.result = result;
} @Override
public String toString() {
return "CommonWithObject [code=" + code + ", describe=" + describe + ", result=" + result + "]";
} }
package gsondemo; import com.google.gson.Gson; public class WithArrayAna { public static void main(String[] args) {
String json = "{\"code\":\"S0000\","
+" \"describe\":\"数据正常返回\","
+" \"result\":[{\"accountBlank\":\"xxxxxx\","
+" \"bankAccount\":\"xxxxxx\","
+" \"code\":\"xxx\","
+" \"id\":\"xxxxxxx\","
+" \"kpAddr\":\"xxxxxx\","
+" \"kpCode\":\"xxxxx\","
+" \"kpName\":\"xxxxx\","
+" \"kpTel\":\"xxx\"}]}";
Gson gson = new Gson();
CommonWithArray commonWithArray = gson.fromJson(json, CommonWithArray.class);
System.out.println(commonWithArray);
} }
主要区别就是bean类的区别,根据解析的情况组合所需要的bean
Java--Json解析的更多相关文章
- Jackson:我是最牛掰的 Java JSON 解析器(有点虚)
在当今的编程世界里,JSON 已经成为将信息从客户端传输到服务器端的首选协议,可以好不夸张的说,XML 就是那个被拍死在沙滩上的前浪. 很不幸的是,JDK 没有 JSON 库,不知道为什么不搞一下.L ...
- JAVA JSON解析:类XPATH解析JSON
目前JAVA解析JSON的方式有很多种,json-lib啊,GJSON啊,等等都可以解析,但通常是将JSON转换为对象或者是LIST或者是MAP,对于我们测试人员来说,其实我们并不需要里面的全部信息, ...
- java json解析(转)
转自:https://www.cnblogs.com/sunnywindycloudy/p/8343013.html 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是XML格式 ...
- Java进阶学习:JSON解析利器JackSon
Java:JSON解析利器JackSon JackSon基础 1.Maven项目引入 <!-- https://mvnrepository.com/artifact/org.codehaus.j ...
- Json解析工具Jackson(简单应用)
原文http://blog.csdn.net/nomousewch/article/details/8955796 概述 Jackson库(http://jackson.codehaus.org),是 ...
- Java的Json解析包FastJson使用
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...
- Java下利用Jackson进行JSON解析和序列化
Java下利用Jackson进行JSON解析和序列化 Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...
- java json 的生成和解析 --json-lib
类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; im ...
- 你必须掌握的Java基础:JSON解析工具-json-lib
一.简介 json-lib是一个Java类库,提供将Java对象,包括beans,maps,collections,java arrays和xml等转换成JSON,或者反向转换的功能. 二.准备 在 ...
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
随机推荐
- express连接数据库 读取表
connection 连接数据库 connection.query 查询表 1.依赖 const mysql = require('mysql'); 连接数据库代码 var connecti ...
- office(CVE-2012-0158)漏洞分析报告
2019/9/12 1.漏洞复现 ①发现崩溃 ②找到漏洞所在的函数,下断点,重新跑起来,单步调试,找到栈被改写的地方 ③分析该函数 把MSCOMCTL拖入IDA,查看该函数代码 ④查看调用栈,回溯. ...
- Hibernate--(二)增删改查
1.增删改查: public class Test { public static void main(String[] args) { SessionFactory sf = new Configu ...
- Axure下载,附注册码
下载地址:https://www.axure.com.cn/3510/ 汉化教程:https://www.axure.com.cn/2616/ 注册码: Axure RP 8.1.0.3377--可用 ...
- 汉诺塔(思维、DP思想)
链接:https://ac.nowcoder.com/acm/contest/3007/C来源:牛客网 题目描述 现在你有 N 块矩形木板,第 i 块木板的尺寸是 Xi*Yi,你想用这些木板来玩汉诺塔 ...
- 当spring单元测试需要用到临时表的时候
需要将整个单元测试的方法交给spring的事务管理器控制. 两种解决方法: 1.加载的spring配置文件中advice要切到需要测试的方法. 2.单元测试类继承AbstractTransaction ...
- django数据库读写分离
django数据库读写分离 1. 配置数据库 settings.py文件中 用SQLite: DATABASES = { 'default': { 'ENGINE': 'django.db.backe ...
- POJ 1731:Orders
Orders Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9940 Accepted: 6048 Descriptio ...
- POJ 1942:Paths on a Grid
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22918 Accepted: 5651 ...
- 51nod 1421:最大MOD值
1421 最大MOD值 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以 ...