Java解析复杂JSON数据的一种方法

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);



 

更多学习资料可关注:gzitcast
//方法很多,这里仅提供其中一种

发布了837 篇原创文章 · 获赞 4 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/u010395024/article/details/105087361