1.需解析JSON数据:
{
"code": 0,
"message": "success",
"sid": "igr0007e88e@dx16e35035e9e020d802",
"data": {
"result": {
"age": {
"age_type": "0",
"child": "0.1452",
"middle": "0.5371",
"old": "0.3177"
},
"gender": {
"female": "0.1970",
"gender_type": "1",
"male": "0.8030"
}
},
"status": 2
}
} 2.需求:取出gender_type值;
3.
import org.json.JSONObject;
public void dealJson(String text){
JSONObject jsonObject = new org.json.JSONObject(text); //将String类型的数据转换为JSONObject对象
JSONObject dataJson = jsonObject.getJSONObject("data"); //使用getJSONObject方法取出data的JSONObject对象
JSONObject resultJson = dataJson.getJSONObject("result");
JSONObject genderJson = resultJson.getJSONObject("gender");
System.out.println("genderJson==>"genderJson);
String gender = genderJson.getString("gender_type"); //取出gender_type的值
System.out.println("gender==>"+gender);
}
//方法很多,这里仅提供其中一种
|