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;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章