Android==>JSON解析

來源:互聯網
上載者:User

標籤: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;
}
}
}

Android==>JSON解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.