标签:
-------》JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。
1:jar包
地址:
2:示例代码
目的:==》获取当前新闻分类的列表信息:
==》获取json网址:
http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41
json源:
{
"status": "ok",
"paramz": {
"feeds": [
{
"id": 280247,
"oid": 410,
"category": "topic",
"data": {
"subject": "天津爆炸死亡人数增至44人",
"summary": "截至今日12时,此次爆炸事故共造成44人死亡,其中包括12名消防官兵。",
"cover": "/Attachs/Topic/410/1c11cf67c4944973afbe9fd16a352927_padmini.JPG",
"format": "topic",
"changed": "2015-08-13 06:40:28"
}
},
{
"id": 280683,
"oid": 271235,
"category": "article",
"data": {
"subject": "天津爆炸事故新闻发布会汇总",
"summary": "今天下午4点30分,在滨海新区美华大酒店召开仓库爆炸事故新闻发布会。",
"cover": "/Attachs/Article/271235/147eeab6e9d74494a212a7f29ac9225f_padmini.JPG",
"pic": "",
"format": "txt",
"changed": "2015-08-13 18:15:39"
}
},
{
"id": 280237,
"oid": 270764,
"category": "article",
"data": {
"subject": "对天津滨海爆炸作指示",
"summary": "对天津滨海新区危险品仓库爆炸作出重要指示,要求全力救治伤员。",
"cover": "/Attachs/Article/270764/cde97c0e7430479baf98cd399316d9ee_padmini.JPG",
"pic": "",
"format": "txt",
"changed": "2015-08-13 07:18:50"
}
}
],
"PageIndex": 1,
"PageSize": 3,
"TotalCount": 50659,
"TotalPage": 16887
}
}
解析代码:
package json; import java.util.List; public class Paramz { private List<Feeds> list; private int pageIndex; private int pageSize; private int totalCount; private int totalPage; public Paramz() { super(); } public Paramz(List<Feeds> list, int pageIndex, int pageSize, int totalCount, int totalPage) { super(); this.list = list; this.pageIndex = pageIndex; this.pageSize = pageSize; this.totalCount = totalCount; this.totalPage = totalPage; } public List<Feeds> getList() { return list; } public void setList(List<Feeds> list) { this.list = list; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } @Override public String toString() { return "今日新闻:\r\n" + getListString() + "\r\n" + "-----------------------------------------------------------------------\r\n" + "页码:" + pageIndex + " 新闻条数:" + pageSize + " 总条数:" + totalCount + " 总页数:" + totalPage + " "; } public String getListString() { String str = ""; for (Feeds f : list) { str += f; } return str; } }
package json; public class Feeds { private int id; private int oid; private String category; private Data data; public Feeds() { super(); } public Feeds(int id, int oid, String category, Data data) { super(); this.id = id; this.oid = oid; this.category = category; this.data = data; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getOid() { return oid; } public void setOid(int oid) { this.oid = oid; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public Data getData() { return data; } public void setData(Data data) { this.data = data; } @Override public String toString() { return "-----------------------------------------------------------------------\r\n" + "id:" + id + "\r\n" + "oid:" + oid + "\r\n" + "category:" + category + "\r\n" + data + "\r\n"; } }
package json; public class Data { private String subject; private String summary; private String cover; private String format; private String changed; public Data() { super(); } public Data(String subject, String summary, String cover, String format, String changed) { super(); this.subject = subject; this.summary = summary; this.cover = cover; this.format = format; this.changed = changed; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getCover() { return cover; } public void setCover(String cover) { this.cover = cover; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } public String getChanged() { return changed; } public void setChanged(String changed) { this.changed = changed; } @Override public String toString() { return " 标题:" + subject + "\r\n" + " 摘要:" + summary + "\r\n" + " cover:" + cover + "\r\n" + " format:" + format + "\r\n" + " 发布时间:" + changed + "\r\n"; } }
package json; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class HttpUtils { public static String getJsonStr(String path) throws IOException { String jsonStr = null; URL url = new URL(path); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(5000); con.setDoInput(true); InputStream in = null; if (con.getResponseCode() == 200) { in = con.getInputStream(); jsonStr = getResult(in); } return jsonStr; } private static String getResult(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = 0; byte[] arr = new byte[1024]; while ((len = in.read(arr)) != -1) { bos.write(arr,0,len); } String str = new String(bos.toByteArray(),"utf-8"); return str; } public static Paramz getParamz(String path) throws IOException, JSONException { Paramz paramz = new Paramz(); String json = getJsonStr(path); JSONObject objJson = new JSONObject(json); JSONObject paramzObj = objJson.getJSONObject("paramz"); paramz.setPageIndex(paramzObj.getInt("PageIndex")); paramz.setPageSize(paramzObj.getInt("PageSize")); paramz.setTotalCount(paramzObj.getInt("TotalCount")); paramz.setTotalPage(paramzObj.getInt("TotalPage")); JSONArray array = paramzObj.getJSONArray("feeds"); List<Feeds> list = new ArrayList<Feeds>(); for(int i = 0;i<array.length();i++) { JSONObject obj = array.getJSONObject(i); Feeds feed = new Feeds(); feed.setId(obj.getInt("id")); feed.setOid(obj.getInt("oid")); feed.setCategory(obj.getString("category")); JSONObject dataObj = obj.getJSONObject("data"); Data data = new Data(); data.setSubject(dataObj.getString("subject")); data.setSummary(dataObj.getString("summary")); data.setCover(dataObj.getString("cover")); data.setFormat(dataObj.getString("format")); data.setChanged(dataObj.getString("changed")); feed.setData(data); list.add(feed); } paramz.setList(list); return paramz; } }
package json; import java.io.IOException; import org.json.JSONException; public class Test { public static void main(String[] args) throws IOException, JSONException { String path = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=100&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41"; Paramz paramz = HttpUtils.getParamz(path); System.out.println(paramz); } }
标签:
原文地址:http://www.cnblogs.com/lipeng0824/p/4728257.html