Eg:
**
**
{
"state": "00",
"monitor_id": "201411001",
"monitor_name": "1",
"dispatchtime": "",
"monitorstate": "",
"monitordes": "",
"results": [
{
"turbidity": [
{
"turbidity": "23424",
"turbidity_time": "20160708 12:00:00"
},
{
"turbidity": "356456",
"turbidity_time": "20160709 12:00:00"
}
],
"chlorinedioxide": [
{
" chlorinedioxide ": "23424",
" chlorinedioxide_time ": "20160708 12:00:00"
},
{
" chlorinedioxide ": "899489",
" chlorinedioxide_time ": "20160710 12:00:00"
}
]
}
]
}
{
"state": "00",
"monitor_id": "201411001",
"monitor_name": "1",
"dispatchtime": "",
"monitorstate": "",
"monitordes": "",
"data": [
{
"turbidity": [
{
"turbidity": "23424",
"turbidity_time": "20160708 12:00:00"
},
{
对于这个Json,要先建三层的Bean类,具体代码如下:
package com.li.json;
import java.util.List;
public class HistoryDataBean {
private String state;
private String monitor_id;
private String monitor_name;
private String dispatchtime;
private String monitorstate;
private String monitordes;
private List data;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getMonitor_id() {
return monitor_id;
}
public void setMonitor_id(String monitor_id) {
this.monitor_id = monitor_id;
}
public String getMonitor_name() {
return monitor_name;
}
public void setMonitor_name(String monitor_name) {
this.monitor_name = monitor_name;
}
public String getDispatchtime() {
return dispatchtime;
}
public void setDispatchtime(String dispatchtime) {
this.dispatchtime = dispatchtime;
}
public String getMonitorstate() {
return monitorstate;
}
public void setMonitorstate(String monitorstate) {
this.monitorstate = monitorstate;
}
public String getMonitordes() {
return monitordes;
}
public void setMonitordes(String monitordes) {
this.monitordes = monitordes;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
@Override
public String toString()
{
return "Status [state=" + state + ", monitor_id=" + monitor_id
+ ", monitor_name=" + monitor_name + ", dispatchtime=" + dispatchtime
+ ", monitorstate=" + monitorstate + ", monitordes=" + monitordes
+ ", data=" + data + "]";
}
}
package com.li.json;
import java.util.List;
public class Data {
private List turbidity;
private List chlorinedioxides;
public List getTurbidity() {
return turbidity;
}
public void setTurbidity(List turbidity) {
this.turbidity = turbidity;
}
public List getChlorinedioxides() {
return chlorinedioxides;
}
public void setChlorinedioxides(List chlorinedioxides) {
this.chlorinedioxides = chlorinedioxides;
}
@Override
public String toString() {
return "Data [turbidity"+turbidity +",chlorinedioxides"+chlorinedioxides+"]";
}
}
package com.li.json;
public class Turbidity {
private String turbidity;
private String turbidity_time;
public String getTurbidity() {
return turbidity;
}
public void setTurbidity(String turbidity) {
this.turbidity = turbidity;
}
public String getTurbidity_time() {
return turbidity_time;
}
public void setTurbidity_time(String turbidity_time) {
this.turbidity_time = turbidity_time;
}
@Override
public String toString() {
return "Turbidity [turbidity"+turbidity +",turbidity_time"+turbidity_time+"]";
}
}
package com.li.json;
public class Chlorinedioxide {
private String chlorinedioxide;
private String chlorinedioxide_time;
public String getChlorinedioxide() {
return chlorinedioxide;
}
public void setChlorinedioxide(String chlorinedioxide) {
this.chlorinedioxide = chlorinedioxide;
}
public String getChlorinedioxide_time() {
return chlorinedioxide_time;
}
public void setChlorinedioxide_time(String chlorinedioxide_time) {
this.chlorinedioxide_time = chlorinedioxide_time;
}
@Override
public String toString() {
return "Chlorinedioxide [chlorinedioxide"+chlorinedioxide +",chlorinedioxide_time"+chlorinedioxide_time+"]";
}
}
接下来一切都简单了
/*建一个Gson对象*/
Gson gson=new Gson();
HistoryDataBean bean=gson.fromJson(toJson(), HistoryDataBean.class);
/*接下来想要什么数据都可以直接提取了,如下想要获取第一组数据二氧化硫的时间:*/
System.out.println(bean.getData().get(0).getChlorinedioxides().get(0).getChlorinedioxide_time());