JSON字符串解析
接上篇。主要是包装了一下Android自带的JSON解析的API,用于HTTP通信。
代码
JSONParser.java
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
/**
* Created by song on 2017/4/6.
*/
public class JSONParser {
/**
* 从json字符串取key对应的String类型value
* @param str json字符串
* @param key key
* @return String类型value
*/
public static String GetValue(String str, String key) {
try {
if(str != null && key != null) {
JSONObject jsonObject = new JSONObject(str);
return jsonObject.getString(key);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 从json字符串取key对应的int类型value
* @param str json字符串
* @param key key
* @return int类型value
*/
public static int GetInt(String str, String key) {
try {
if(str != null && key != null) {
JSONObject jsonObject = new JSONObject(str);
return jsonObject.getInt(key);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
/**
* 从json字符串取key对应的long类型value
* @param str json字符串
* @param key key
* @return long类型value
*/
public static long GetLong(String str, String key) {
try {
if(str != null && key != null) {
JSONObject jsonObject = new JSONObject(str);
return jsonObject.getLong(key);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
/**
* 从json字符串取key对应的double类型value
* @param str json字符串
* @param key key
* @return double类型value
*/
public static double GetDouble(String str, String key) {
try {
if(str != null && key != null) {
JSONObject jsonObject = new JSONObject(str);
return jsonObject.getDouble(key);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
/**
* 从json字符串取key对应的JSONArray
* @param str json字符串
* @param key key
* @return JSONArray
*/
public static JSONArray GetArray(String str, String key) {
try {
if(str != null && key != null) {
JSONObject jsonObject = new JSONObject(str);
return jsonObject.getJSONArray(key);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* json字符串转JSONArray
* @param str json字符串
* @return JSONArray
*/
public static JSONArray GetArray(String str) {
try {
if(str != null) {
return new JSONArray(str);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 从JSONArray取index对应的int类型value
* @param jsonArray json数组
* @param index 索引
* @return int类型value
*/
public static int GetIntFromJSONArray(JSONArray jsonArray, int index) {
try {
if(jsonArray != null) {
return jsonArray.getInt(index);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
/**
* 从jsonArray取index对应的String类型value
* @param jsonArray json数组
* @param index 索引
* @return String类型value
*/
public static String GetValueFromJSONArray(JSONArray jsonArray, int index) {
try {
if(jsonArray != null) {
return jsonArray.getString(index);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 从jsonArray取所有String类型value
* @param jsonArray json数组
* @return String数组
*/
public static String [] GetValuesFromJSONArray(JSONArray jsonArray) {
try {
if(jsonArray != null) {
int size = jsonArray.length();
String [] strRet = new String[size];
for (int i = 0; i < size; ++i){
strRet[i] = jsonArray.getString(i);
}
return strRet;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
说明
- 因为很多地方都要用到,为了避免到处都是try catch,所以包了一层,方便调用。
- 前几个Get函数好像可以合并成一个函数
public static Object GetValue(String str, String key) {
try {
if(str != null && key != null) {
JSONObject jsonObject = new JSONObject(str);
return jsonObject.get(key);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}