Json数据示例:
{"product":{"result":"1","data":{"value":"4"},"costTime":15,"operation":"3"},"code":600000,"flag":{"flag_telperiod":1},"swift_number":"3100034_20161221153228_0705","exceptions":[]}
目的,要获取value:
方案:
//第一种方式
/*JSONObject json = JSONObject.fromObject(obj);
String product = json.optString("product");
json = JSONObject.fromObject(product);
product = json.optString("data");
json = JSONObject.fromObject(product);
product = json.optString("value");
int value = Integer.parseInt(product.toString());*/
//第二种方式
JSONObject jsonObject = new JSONObject().fromObject(obj.toString());
Object data=jsonObject.get("product");
jsonObject = new JSONObject().fromObject(data.toString());
data=jsonObject.get("data");
jsonObject = new JSONObject().fromObject(data.toString());
data=jsonObject.get("value");
int value = Integer.parseInt(data.toString());