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;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值