中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android開發(fā)中json數(shù)據(jù)解析工具類

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
用Android自帶的JSONArray及JSONObject處理json數(shù)據(jù)----------------------------------------------------------------

在下面方法中傳入的String path是數(shù)據(jù)所在的服務(wù)器url地址

public class dealJsonUtil {

/*
 * 獲取"數(shù)組形式"的JSON數(shù)據(jù),
 */
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對(duì)象,從網(wǎng)絡(luò)中獲取網(wǎng)頁數(shù)據(jù)*/
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
/**設(shè)置超時(shí)時(shí)間為5秒*/
conn.setConnectTimeout(5 * 1000);
/**HttpURLConnection是通過HTTP協(xié)議請(qǐng)求path路徑的,所以需要設(shè)置請(qǐng)求方式,可以不設(shè)置,因?yàn)槟J(rèn)為GET*/
conn.setRequestMethod("GET");
/**判斷請(qǐng)求是否成功,成功時(shí)請(qǐng)求碼為200,否則失敗*/
if (conn.getResponseCode() == 200) {
/**獲取數(shù)據(jù)輸入流*/
InputStream is = conn.getInputStream();
/**把輸入流轉(zhuǎn)換成字符數(shù)組*/
byte[] data = readStream2Array(is);
/**字符數(shù)組轉(zhuǎn)換成字符串*/
json = new String(data);
/*
 * 數(shù)據(jù)形式:[{"stuNo":100,"name":"小明"},{"stuNo":101,"name":"小張"}]數(shù)據(jù)為數(shù)組形式,直接用 android框架                                           JSONArray讀取數(shù)據(jù),轉(zhuǎn)換成Array
 */
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
/**獲取每條數(shù)據(jù)中的對(duì)象*/
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;
}


/**    
 * 獲取"對(duì)象形式"的JSON數(shù)據(jù),    
 *  
 * @param path  網(wǎng)頁路徑    
 * @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); // 單位是毫秒,設(shè)置超時(shí)時(shí)間為5秒
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream(); 
byte[] data = readStream2Array(is);
String json = new String(data);
/*
 * 數(shù)據(jù)形式:{"total":2,"success":true,"arrayData":[{"id":1,"name":"小豬"},{"id":2,"name":"小貓"}]}   
 * 返回的數(shù)據(jù)形式是一個(gè)Object類型,所以可以直接轉(zhuǎn)換成一個(gè)Object*/
JSONObject jsonObject = new JSONObject(json);
int total = jsonObject.getInt("total");
Boolean success = jsonObject.getBoolean("success");
/**json對(duì)象中有一個(gè)數(shù)組數(shù)據(jù),又可以使用getJSONArray獲取數(shù)組*/
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;
}


/**    
 * 獲取類型復(fù)雜的JSON數(shù)據(jù)      
 */
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);
/*
 * 數(shù)據(jù)形式: {"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;
}


/**    
 * 把輸入流轉(zhuǎn)換成字符數(shù)組       
 */
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();
}
}

標(biāo)簽: 服務(wù)器 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php RAS加密類代碼

下一篇:使用JdbcTemplate操作數(shù)據(jù)