import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
-
The class JSONUtils present …
-
2009-3-10 12:02:57
-
@author Zhou Weijun
-
@since 1.00.000
/
public class JSONUtils {
private static Log log = LogFactory.getLog(JSONUtils.class);
/*- @param jsonObject The jsonObject.
- @param fieldName The fieldName.
- @return The value.
*/
public static String readString(JSONObject jsonObject, String fieldName) {
try {
return jsonObject.getString(fieldName);
} catch (Exception e) {
// log.warn(“readString is null”);
}
return null;
}
/**
- @param jsonObject The jsonObject.
- @param fieldName The fieldName.
- @return The value.
*/
public static int readInteger(JSONObject jsonObject, String fieldName) {
try {
return jsonObject.getInt(fieldName);
} catch (Exception e) {
// log.warn(“readInteger is null”);
return 0;
}
}
/**
- @param jsonObject The jsonObject.
- @param fieldName The fieldName.
- @return The value.
*/
public static JSONArray readJSONArray(JSONObject jsonObject, String fieldName) {
try {
return jsonObject.getJSONArray(fieldName);
} catch (Exception e) {
// log.warn(“readJSONArray is null”);
return new JSONArray();
}
}
/**
- @param jsonObject The jsonObject.
- @param fieldName The fieldName.
- @return The value.
*/
public static JSONObject readJSONObject(JSONObject jsonObject, String fieldName) {
try {
jsonObject = jsonObject.getJSONObject(fieldName);
} catch (Exception e) {
// log.warn(“readJSONObject is null”);
return new JSONObject();
}
return jsonObject;
}
}