在android中从JSON中获取值

nbnkbykc  于 2023-05-02  发布在  Android
关注(0)|答案(2)|浏览(133)

我向服务器发送请求,它给了我一个响应,并以JSON格式提供数据,现在我想从JSON格式中获取一些特定的值,所以要这样做。

{
"education": [
{
  "school": {
    "id": "2009305", 
    "name": "FG boys public high school Bannu Cantt "
  }, 
  "type": "High School"
}, 
{
  "school": {
    "id": "109989", 
    "name": "University of Engineering & Technology"
  }, 
  "type": "College"
}
], 
"id": "xxxxxxx"
}

现在我需要这个JSON中的学校名称

hsgswve4

hsgswve41#

JSONObject json = new JSONObject(response);
JSONArray education = json.getJSONArray("education");
for(int i = 0; i < education.length(); i++){
     JSONObject con_json = education.getJSONObject(i);
     String school_type = con_json.getString("type");
     JSONObject school_json = con_json.getJSONObject("school");
     String school_name = school_json.getString("name");
}
8qgya5xd

8qgya5xd2#

首先从你的数据构建一个JSON对象:

JSONObject jsonObj = new JSONObject(result); //result = your Data String in fetched from server

然后你就可以用它的密钥来检索你想要的东西。例如:

jsonObj.getString("id"); // it returns "xxxxxxx". as is in your data

相关问题