Android json数据解析
前言
什么是json
Android中json相关解析类
JSONObject
value的类型包括: JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL
- get():在确定key存在的条件下使用,否则当无法检索到相关key时,将会抛出一个Exception异常信息
- opt():这个方法相对比较灵活,当无法获取所指定数值时,将会返回一个默认值,并不会抛出异常(个人推荐使用这个方法)
JSONArray
同样JSONArray的value类型可以包括:JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL
Android解析实例
获取json字符串
private String getJsonByNetwork() { // You can get json by this url final String url = "http://api.androidhive.info/contacts/"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); InputStream inputStream = null; String result = null; try { HttpResponse response = httpClient.execute(httpGet); inputStream = response.getEntity().getContent(); // Json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream, "UTF-8")); StringBuilder sb = new StringBuilder(); String tmp = null; while ((tmp = reader.readLine()) != null) { sb.append(tmp); } result = sb.toString(); } catch (Exception e) { try { if (inputStream != null) { inputStream.close(); } } catch (IOException se) { } } return result; }
解析json字符串
// Create a JSONObject JSONObject jsonObject = new JSONObject(result);
(2) 获取JSONArray,遍历JSONArray数组
// To get a specific JSONArray JSONArray jsonArray = jsonObject.getJSONArray("contacts"); // To get items from the array for (int i = 0; i < jsonArray.length(); i ++) { // TODO:Traverse the jsonarray }
// Create a JSONObject JSONObject jsonObject = new JSONObject(result); // To get a specific JSONArray JSONArray jsonArray = jsonObject.getJSONArray("contacts"); // To get items from the array for (int i = 0; i < jsonArray.length(); i++) { // To get a specific JSONObject JSONObject oneObject = jsonArray.getJSONObject(i); }
(4)获取特定字符串
// Create a JSONObject JSONObject jsonObject = new JSONObject(result); // To get a specific JSONArray JSONArray jsonArray = jsonObject.getJSONArray("contacts"); // To get items from the array for (int i = 0; i < jsonArray.length(); i++) { // To get a specific JSONObject JSONObject oneObject = jsonArray.getJSONObject(i); // To get a specific string String id = oneObject.getString("id"); String name = oneObject.getString("name"); Log.e("wzy", "id is:" + id + ", name is " + name); }
解析结果:
03-05 10:26:08.690: E/wzy(26401): id is:c200, name is Ravi Tamada 03-05 10:26:08.690: E/wzy(26401): id is:c201, name is Johnny Depp 03-05 10:26:08.690: E/wzy(26401): id is:c202, name is Leonardo Dicaprio 03-05 10:26:08.690: E/wzy(26401): id is:c203, name is John Wayne 03-05 10:26:08.691: E/wzy(26401): id is:c204, name is Angelina Jolie 03-05 10:26:08.691: E/wzy(26401): id is:c205, name is Dido 03-05 10:26:08.691: E/wzy(26401): id is:c206, name is Adele 03-05 10:26:08.692: E/wzy(26401): id is:c207, name is Hugh Jackman 03-05 10:26:08.693: E/wzy(26401): id is:c208, name is Will Smith 03-05 10:26:08.693: E/wzy(26401): id is:c209, name is Clint Eastwood 03-05 10:26:08.694: E/wzy(26401): id is:c2010, name is Barack Obama 03-05 10:26:08.694: E/wzy(26401): id is:c2011, name is Kate Winslet 03-05 10:26:08.695: E/wzy(26401): id is:c2012, name is Eminem
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。