上次我们讲到XML解析网络数据,这次我们来研究研究用Json格式解析数据
照旧,我们先在web端的FQAcgtion.class模拟一组Json格式的数据:
- public String getJson() throws Exception {
- // 获取数据
- // 调用数据库查询数据,返回对象集合(….)
- List<FQ> fqs = new ArrayList<FQ>();
- for (int i = 1; i <= 100; i++) {
- Calendar calendar = Calendar.getInstance();
- int year = calendar.get(Calendar.YEAR);
- int month = calendar.get(Calendar.MONTH);
- int day = calendar.get(Calendar.DAY_OF_MONTH);
- fqs.add(new FQ(“原生态” + i, “很纯”, year + “-“ + month + “-“ + day));
- }
- //将对象集合转换为json,例如:{“class”:”150831”,”lists”:30,”fqs”“[{},{},{}]}
- JSONObject jo=new JSONObject();
- jo.put(”clazz”, “150831”);
- jo.put(”lists”, fqs.size());
- JSONArray ja=new JSONArray();
- for (FQ fq : fqs) {
- JSONObject jos=new JSONObject();
- jos.put(”name”, fq.getName());
- jos.put(”content”, fq.getContent());
- jos.put(”time”, fq.getTime());
- ja.add(jos);
- }
- jo.put(”fqs”, ja);
- // 将对象集合存放到请求域中
- // ServletActionContext.getRequest().setAttribute(“fqs”, jo.toString());
- return “dataResultJson”;
- }
public String getJson() throws Exception {
// 获取数据
// 调用数据库查询数据,返回对象集合(....)
List<FQ> fqs = new ArrayList<FQ>();
for (int i = 1; i <= 100; i++) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
fqs.add(new FQ("原生态" + i, "很纯", year + "-" + month + "-" + day));
}
//将对象集合转换为json,例如:{"class":"150831","lists":30,"fqs""[{},{},{}]}
JSONObject jo=new JSONObject();
jo.put("clazz", "150831");
jo.put("lists", fqs.size());
JSONArray ja=new JSONArray();
for (FQ fq : fqs) {
JSONObject jos=new JSONObject();
jos.put("name", fq.getName());
jos.put("content", fq.getContent());
jos.put("time", fq.getTime());
ja.add(jos);
}
jo.put("fqs", ja);
// 将对象集合存放到请求域中
// ServletActionContext.getRequest().setAttribute("fqs", jo.toString());
return "dataResultJson";
}
dataResultJson.jsp:
- <%@ page language=“java” contentType=“text/plain; charset=utf-8” pageEncoding=“utf-8”%>{fqs} </span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets_01.png"></a></div></div><pre name="code" class="java" style="display: none;"><%@ page language="java" contentType="text/plain; charset=utf-8" pageEncoding="utf-8"%>{fqs}
struts.xml配置:- <action name=“fqAction*” class=“com.zking.action.FQAcgtion” method=“{1}”>
- <result name=”dataResult”>/dataResult.jsp</result>
- <result name=”dataResult”>/dataResultJson.jsp</result>
- </action>
<action name="fqAction*" class="com.zking.action.FQAcgtion" method="{1}"> <result name="dataResult">/dataResult.jsp</result> <result name="dataResult">/dataResultJson.jsp</result> </action>
截图如下:
数据模拟好了,那我们现在来Android端拿取数据
首先,先来个XML页面:依然是一个按钮和一个listview展示数据
activity_get_json.xml
- <Button
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:text=”获取Json”
- android:onClick=”getJson”
- />
- <ListView
- android:layout_width=”match_parent”
- android:layout_height=”wrap_content”
- android:id=”@+id/lv_json_list” </