Android開發中json數據解析工具類
用Android自帶的JSONArray及JSONObject處理json數據----------------------------------------------------------------
在下面方法中傳入的String path是數據所在的服務器url地址
public class dealJsonUtil {
/獲取"數組形式"的JSON數據,
/
public static List<Map<String, String>> getJSONArray(String path) throws Exception {
String json = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = null;
URL url = new URL(path);
/**HttpURLConnection對象,從網絡中獲取網頁數據/
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
/設置超時時間為5秒/
conn.setConnectTimeout(5 1000);
/HttpURLConnection是通過HTTP協議請求path路徑的,所以需要設置請求方式,可以不設置,因為默認為GET/
conn.setRequestMethod("GET");
/**判斷請求是否成功,成功時請求碼為200,否則失敗/
if (conn.getResponseCode() == 200) {
/獲取數據輸入流*/
InputStream is = conn.getInputStream();
/把輸入流轉換成字符數組/
byte[] data = readStream2Array(is);
/**字符數組轉換成字符串/
json = new String(data);
/
數據形式:[{"stuNo":100,"name":"小明"},{"stuNo":101,"name":"小張"}]數據為數組形式,直接用 android框架 JSONArray讀取數據,轉換成Array
/
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
/**獲取每條數據中的對象/
JSONObject item = jsonArray.getJSONObject(i);
/注意key值要一致*/
int id = item.getInt("stuNo");
String name = item.getString("name");
map = new HashMap<String, String>();
map.put("stuNo", id + "");
map.put("name", name);
list.add(map);
}
}
return list;
}
/
獲取"對象形式"的JSON數據,
@param path 網頁路徑
@return 返回List
@throws Exception
/
public static List<Map<String, String>> getJSONObject(String path) throws Exception {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = null;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 1000); // 單位是毫秒,設置超時時間為5秒
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
byte[] data = readStream2Array(is);
String json = new String(data);
/
數據形式:{"total":2,"success":true,"arrayData":[{"id":1,"name":"小豬"},{"id":2,"name":"小貓"}]}
返回的數據形式是一個Object類型,所以可以直接轉換成一個Object/
JSONObject jsonObject = new JSONObject(json);
int total = jsonObject.getInt("total");
Boolean success = jsonObject.getBoolean("success");
/**json對象中有一個數組數據,又可以使用getJSONArray獲取數組/
JSONArray jsonArray = jsonObject.getJSONArray("arrayData");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
int id = item.getInt("id");
String name = item.getString("name");
map = new HashMap<String, String>();
map.put("id", id + "");
map.put("name", name);
list.add(map);
}
}
return list;
}
/
獲取類型復雜的JSON數據
/
public static List<Map<String, String>> getComplexJSON(String path) throws Exception {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = null;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 1000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
byte[] data = readStream2Array(is);
String json = new String(data);
/
數據形式: {"name":"小豬", "age":23, "content":{"questionsTotal":2, "questions": [ { "question": "what's your name?", "answer": "小豬"}, {"question": "what's your age", "answer": "23"}] } }
/
JSONObject jsonObject = new JSONObject(json);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
Log.i("abc", "name:" + name + " | age:" + age);
JSONObject contentObject = jsonObject.getJSONObject("content");
String questionsTotal = contentObject.getString("questionsTotal");
JSONArray contentArray = contentObject.getJSONArray("questions");
for (int i = 0; i < contentArray.length(); i++) {
JSONObject item = contentArray.getJSONObject(i);
String question = item.getString("question");
String answer = item.getString("answer");
map = new HashMap<String, String>();
map.put("question", question);
map.put("answer", answer);
list.add(map);
}
}
return list;
}
/
把輸入流轉換成字符數組
/
public static byte[] readStream2Array(InputStream inputStream) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
bout.write(buffer, 0, len);
}
bout.close();
inputStream.close();
return bout.toByteArray();
}
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!