Android==>JSON解析

public class JsonUtil {


/**
* 獲取"數組形式"的JSON數據, 數據形式:[{"id":1,"name":"小名"},{"id":2,"name":"小麗"}]

* @param path
*            網頁路徑
* @return 返回List
* @throws Exception
*/
public static String getJSONArray(String path) throws Exception {
String json = null;
URL url = new URL(path);
// Log.d("","*********************url="+url+"**************************");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 利用HttpURLConnection對象,我們可以從網絡中獲取網頁數據.
conn.setRequestProperty("connection", "Keep-Alive");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
if (conn.getResponseCode() == 200) {// 判斷請求碼是否是200碼,否則失敗
InputStream is = conn.getInputStream(); // 獲取輸入流
byte[] data = readStream(is); // 把輸入流轉換成字符數組
json = new String(data); // 把字符數組轉換成字符串
}
return json;
}


/**
* 把輸入流轉換成字符數組

* @param inputStream
*            輸入流
* @return 字符數組
* @throws Exception
*/
public static byte[] readStream(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();
}

/**
* 檢查jsonobject中是否存在相應字段的int,如果存在就返回對應值

* @param object
* @param val
* @return
* @throws org.json.JSONException
*/
public static int checkAndGetInt(JSONObject object, String val)
throws JSONException {
try {
if (object.has(val) && object.get(val) != null) {
return object.getInt(val);
} else {
return -1;
}
} catch (JSONException e) {
return -1;
}
}


/**
* 檢查jsonobject中是否存在相應字段的int,如果存在就返回對應值

* @param object
* @param val
* @return
* @throws org.json.JSONException
*/
public static int checkAndGetIntWithDefault(JSONObject object, String val,
int result) throws JSONException {
try {
if (object.has(val) && object.get(val) != null) {
return object.getInt(val);
} else {
return result;
}
} catch (JSONException e) {
return result;
}
}


/**
* 檢查jsonobject中是否存在相應字段的string,如果存在就返回對應值

* @param object
* @param val
* @return
* @throws org.json.JSONException
*/
public static String checkAndGetString(JSONObject object, String val)
throws JSONException {
try {
if (object.has(val) && object.get(val) != null) {
return object.getString(val);
} else {
return "";
}
} catch (JSONException e) {
return "";
}
}


/**
* 檢查jsonobject中是否存在相應字段的string,如果存在就返回對應值,否則返回默認值

* @param object
* @param val
* @param value
* @return
* @throws JSONException
*/
public static String checkAndGetStringWithDefault(JSONObject object,
String val, String value) throws JSONException {
try {
if (object.has(val) && object.get(val) != null) {
return object.getString(val);
} else {
return value;
}
} catch (JSONException e) {
return value;
}
}


/**
* 檢查jsonobject中是否存在對應字段的jsonarray,如果存在返回對應值

* @param object
* @param val
* @return
* @throws org.json.JSONException
*/
public static JSONArray checkAndGetArray(JSONObject object, String val)
throws JSONException {
try {
if (object.has(val) && object.get(val) != null) {
return object.getJSONArray(val);
} else {
return new JSONArray();
}
} catch (JSONException e) {
return new JSONArray();
}
}


/**
* 檢查jsonobject中是否存在對應字段的double,如果存在返回對應值

* @param object
* @param val
* @return
* @throws org.json.JSONException
*/
public static Double checkAndGetDouble(JSONObject object, String val)
throws JSONException {
try {


if (object.has(val) && object.get(val) != null) {
return object.getDouble(val);
} else {
return -1.0;
}
} catch (JSONException e) {
return -1.0;
}
}


/**
* 檢查jsonobject中是否存在對應字段的float,如果存在返回對應值

* @param object
* @param val
* @return
* @throws org.json.JSONException
*/
public static Float checkAndGetFloat(JSONObject object, String val)
throws JSONException {
try {
if (object.has(val) && object.get(val) != null) {
return Float.parseFloat(object.getString(val));
} else {
return -1.0f;
}
} catch (JSONException e) {
return -1.0f;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章