初来乍到,其实我也就是一个刚学没有多久的菜鸟,今天,只是想把自己学到的小知识点分享给大家,如果有错误的地方,还请大家指出。
那么言归正传,这篇博客主要教大家如何对Json数据进行解析。首先先看一段Json数据
{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": {
"yestoday_closePrice": "44.44",
"todayMax": "45.62",
"time": "2016-09-06 18:00:42",
"todayMin": "44.72",
"stockNum": "10045",
"name": "WTI原油(NYMEX原油)",
"diff_num": "0.5",
"nowPrice": "44.94",
"diff_rate": "1.13%",
"today_openPrice": "45.05"
}
}
我们要解析上面的Json数据,首先,观察花括号,由于我们用的Gson数据解析,看到一个花括号,其实就是一个类,也就是一个bean文件,
首先,在我们的项目里,创建一个bean文件,我起名为FirstBean,在这个Bean文件里有showapi_res_code,showapi_res_error,showapi_res_body
这三个属性。
public class FirstBean {
private String showapi_res_code;
public String getShowapi_res_code() {
return showapi_res_code;
}
public void setShowapi_res_code(String showapi_res_code) {
this.showapi_res_code = showapi_res_code;
}
public String getShowapi_res_error() {
return showapi_res_error;
}
public void setShowapi_res_error(String showapi_res_error) {
this.showapi_res_error = showapi_res_error;
}
public SecondBean getShowapi_res_body() {
return showapi_res_body;
}
public void setShowapi_res_body(SecondBean showapi_res_body) {
this.showapi_res_body = showapi_res_body;
}
private String showapi_res_error;
private SecondBean showapi_res_body;
}
注意到,这里面有个SecondBean 这个bean文件就是存放showapi_res_body 这个类的各种属性的,
然后 我们再新建SecondBean文件。
public class SecondBean {
public String getYestoday_closePrice() {
return yestoday_closePrice;
}
public void setYestoday_closePrice(String yestoday_closePrice) {
this.yestoday_closePrice = yestoday_closePrice;
}
public String getTodayMax() {
return todayMax;
}
public void setTodayMax(String todayMax) {
this.todayMax = todayMax;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRet_code() {
return ret_code;
}
public void setRet_code(String ret_code) {
this.ret_code = ret_code;
}
public String getTodayMin() {
return todayMin;
}
public void setTodayMin(String todayMin) {
this.todayMin = todayMin;
}
public String getStockNum() {
return stockNum;
}
public void setStockNum(String stockNum) {
this.stockNum = stockNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDiff_num() {
return diff_num;
}
public void setDiff_num(String diff_num) {
this.diff_num = diff_num;
}
public String getNowPrice() {
return nowPrice;
}
public void setNowPrice(String nowPrice) {
this.nowPrice = nowPrice;
}
public String getDiff_rate() {
return diff_rate;
}
public void setDiff_rate(String diff_rate) {
this.diff_rate = diff_rate;
}
public String getToday_openPrice() {
return today_openPrice;
}
public void setToday_openPrice(String today_openPrice) {
this.today_openPrice = today_openPrice;
}
private String yestoday_closePrice;
private String todayMax;
private String time;
private String ret_code;
private String todayMin;
private String stockNum;
private String name;
private String diff_num;
private String nowPrice;
private String diff_rate;
private String today_openPrice;
}
在这个类里面就存放了第二个花括号里面的所有信息。
解析的过程很简单:Gson gson = new Gson();
final FirstBean firstbean = gson.fromJson(res, FirstBean.class);
两句话完成解析。 解析完成后:txt.setText("今日原油价格:"+firstbean.getShowapi_res_body().getNowPrice());
就可以一直调用get方法来获取你需要的信息。
{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": {
"yestoday_closePrice": "44.44",
"todayMax": "45.62",
"time": "2016-09-06 18:00:42",
"todayMin": "44.72",
"stockNum": "10045",
"name": "WTI原油(NYMEX原油)",
"diff_num": "0.5",
"nowPrice": "44.94",
"diff_rate": "1.13%",
"today_openPrice": "45.05"
}
}
我们要解析上面的Json数据,首先,观察花括号,由于我们用的Gson数据解析,看到一个花括号,其实就是一个类,也就是一个bean文件,
首先,在我们的项目里,创建一个bean文件,我起名为FirstBean,在这个Bean文件里有showapi_res_code,showapi_res_error,showapi_res_body
这三个属性。
public class FirstBean {
private String showapi_res_code;
public String getShowapi_res_code() {
return showapi_res_code;
}
public void setShowapi_res_code(String showapi_res_code) {
this.showapi_res_code = showapi_res_code;
}
public String getShowapi_res_error() {
return showapi_res_error;
}
public void setShowapi_res_error(String showapi_res_error) {
this.showapi_res_error = showapi_res_error;
}
public SecondBean getShowapi_res_body() {
return showapi_res_body;
}
public void setShowapi_res_body(SecondBean showapi_res_body) {
this.showapi_res_body = showapi_res_body;
}
private String showapi_res_error;
private SecondBean showapi_res_body;
}
注意到,这里面有个SecondBean 这个bean文件就是存放showapi_res_body 这个类的各种属性的,
然后 我们再新建SecondBean文件。
public class SecondBean {
public String getYestoday_closePrice() {
return yestoday_closePrice;
}
public void setYestoday_closePrice(String yestoday_closePrice) {
this.yestoday_closePrice = yestoday_closePrice;
}
public String getTodayMax() {
return todayMax;
}
public void setTodayMax(String todayMax) {
this.todayMax = todayMax;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRet_code() {
return ret_code;
}
public void setRet_code(String ret_code) {
this.ret_code = ret_code;
}
public String getTodayMin() {
return todayMin;
}
public void setTodayMin(String todayMin) {
this.todayMin = todayMin;
}
public String getStockNum() {
return stockNum;
}
public void setStockNum(String stockNum) {
this.stockNum = stockNum;
}
public String getName() {
return name;
}
public void setName(String name) {
}
public String getDiff_num() {
return diff_num;
}
public void setDiff_num(String diff_num) {
this.diff_num = diff_num;
}
public String getNowPrice() {
return nowPrice;
}
public void setNowPrice(String nowPrice) {
this.nowPrice = nowPrice;
}
public String getDiff_rate() {
return diff_rate;
}
public void setDiff_rate(String diff_rate) {
this.diff_rate = diff_rate;
}
public String getToday_openPrice() {
return today_openPrice;
}
public void setToday_openPrice(String today_openPrice) {
this.today_openPrice = today_openPrice;
}
private String yestoday_closePrice;
private String todayMax;
private String time;
private String ret_code;
private String todayMin;
private String stockNum;
private String name;
private String diff_num;
private String nowPrice;
private String diff_rate;
private String today_openPrice;
}
在这个类里面就存放了第二个花括号里面的所有信息。
解析的过程很简单:Gson gson = new Gson();
final FirstBean firstbean = gson.fromJson(res, FirstBean.class);
两句话完成解析。 解析完成后:txt.setText("今日原油价格:"+firstbean.getShowapi_res_body().getNowPrice());
就可以一直调用get方法来获取你需要的信息。