日期:2014-05-16  浏览次数:20335 次

Android客户端对服务器返回的json文件进行解析

和解析XML的方式大同小异,只有解析方式存在区别:

/**
	 * 解析服务器返回来的json数据
	 * @param content
	 * @return
	 * @throws Exception
	 */
	private static List<Video> parseJson(String content) throws Exception {
		List<Video> videoList = new ArrayList<Video>();
		JSONArray json = new JSONArray(content);
		for(int i=0;i<json.length();i++) {
			JSONObject item = json.getJSONObject(i);
			int id = item.getInt("id");
			String name = item.getString("name");
			String time = item.getString("time");
			Video video = new Video();
			video.setId(id);
			video.setName(name);
			video.setTime(time);
			videoList.add(video);
		}
		return videoList;
	}

?