解析嵌套的JSON数据?

时间:2017-03-08 09:20:04

标签: android json parsing

我能够解析Json对象但无法解析嵌套的JSON对象。我能够解析到“base64”(在JSON DATA下面)但是无法解析那么。对象中的对象如何解析?

JSON数据

{

    "StdID":1,
    "NAME":"Kirsten Green",
    "PHONENO":"095-517-0049",
    "DOB":"2009-12-28T00:00:00",
    "CLASS":9,
    "GENDER":"M",
    "ADDRESS":"8254 At Ave",
    "NATIONALITY":"Belgium",
    "ENROLLEDYEAR":"2016-04-21T00:00:00",
    "Photo":null,
    "Cat_ID":5,
    "base64":null,
    "studentDetails":{
        "StdID":1,
        "GUARDIAN_PHONE_NO":"002-283-4824",
        "MOBILE_NO":"1-377-762-8548",
        "First_NAME":"Maile",
        "Last_Name":"Lancaster",
        "Relation":"Father",
        "DOB":"2017-02-22T00:00:00",
        "Education":"Ph.D",
        "Occupation":"Etiam ligula tortor,",
        "Income":"20000-30000",
        "Email":"urna@sed.ca",
        "AddLine1":"Ap #416-4247 Sollicitudin Av.",
        "AddLine2":"Ap #801-7380 Imperdiet Avenue",
        "State":"ME",
        "Country":"Israel"
    },
    "Marks":null,
    "stdCategory":{
        "Cat_ID":5,
        "Category":"Normal"
    }

}

家庭班级

 public void makeJsonObjectRequest(int stud_id) {
        String URL = Navigation_URL + stud_id;
        Log.d("TAG", "URL:" + URL);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            studentInformation = new StudentInformation();

                            studentInformation.StdID = String.valueOf(jsonObject.get("StdID"));
                            studentInformation.Name = jsonObject.getString("NAME");
                            studentInformation.Gender = (String) jsonObject.get("GENDER");
                            studentInformation.Phonenumber = String.valueOf(jsonObject.get("PHONENO"));
                            studentInformation.StudentClass = String.valueOf(jsonObject.get("CLASS"));
                            studentInformation.Enrolled_Year = String.valueOf(jsonObject.get("ENROLLEDYEAR"));
                            studentInformation.Address = String.valueOf(jsonObject.get("ADDRESS"));
                            studentInformation.DOB = String.valueOf(jsonObject.get("DOB"));
                            studentInformation.Nationality = String.valueOf(jsonObject.get("NATIONALITY"));
                            profilename.setText(studentInformation.Name);






                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);


    }
  

如何解析嵌套的json?

5 个答案:

答案 0 :(得分:1)

你在JSON对象中使用JSON对象试试这个

JSONObject jsonObject = new JSONObject(response);
JSONObject jsonObject2=jsonObject.getJSONObject("studentDetails");

并尝试从jsonObject2获取studentDetails,并将此类似于“stdCategory”

答案 1 :(得分:1)

您需要解析嵌套对象,如

JSONObject jsonObject = new JSONObject(response);
JSONObject studentDetail = jsonObject.getJSONObject("studentDetails");

然后你可以获得像

这样的值
String.valueOf(studentDetail.get("StdID"));

同样,您可以访问上面的其他嵌套JSON对象

如果您的嵌套JSON对象是一个数组,您需要使用getJSONArray函数

JSONArray array1 = jsonObject.getJSONArray("keyAttribute");

答案 2 :(得分:1)

试试这个:

 try{
    JSONObject json = new JSONObject(jsonString);  //your JSON String
    JSONObject studentDetails = json.getJSONObject("studentDetails");
    String StdID = String.valueOf(studentDetails.getString("StdID"));
    String GUARDIAN_PHONE_NO = String.valueOf( studentDetails.getString("GUARDIAN_PHONE_NO"));
    //rest of the strings
     }
     catch (JSONException e){
           e.printStackTrace();
    }

答案 3 :(得分:1)

试试这个,

        public void makeJsonObjectRequest(int stud_id) {
        String URL = Navigation_URL + stud_id;
        Log.d("TAG", "URL:" + URL);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            studentInformation = new StudentInformation();

                            studentInformation.StdID = String.valueOf(jsonObject.get("StdID"));
                            studentInformation.Name = jsonObject.getString("NAME");
                            studentInformation.Gender = (String) jsonObject.get("GENDER");
                            studentInformation.Phonenumber = String.valueOf(jsonObject.get("PHONENO"));
                            studentInformation.StudentClass = String.valueOf(jsonObject.get("CLASS"));
                            studentInformation.Enrolled_Year = String.valueOf(jsonObject.get("ENROLLEDYEAR"));
                            studentInformation.Address = String.valueOf(jsonObject.get("ADDRESS"));
                            studentInformation.DOB = String.valueOf(jsonObject.get("DOB"));
                            studentInformation.Nationality = String.valueOf(jsonObject.get("NATIONALITY"));
                            profilename.setText(studentInformation.Name);

                            JSONObject studentDetails_obj=jsonObject.getJSONObject("studentDetails");
                            int StdID=studentDetails_obj.getInt("StdID");
                            String GUARDIAN_PHONE_NO=studentDetails_obj.getString("GUARDIAN_PHONE_NO");
                            String MOBILE_NO=studentDetails_obj.getString("MOBILE_NO");
                            String First_NAME=studentDetails_obj.getString("First_NAME");
                            String Last_Name=studentDetails_obj.getString("Last_Name");


                            JSONObject stdCategory_obj=jsonObject.getJSONObject("stdCategory");
                            int Cat_ID=stdCategory_obj.getInt("Cat_ID");
                            String Category=stdCategory_obj.getString("Category");

                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }

                    }

                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(Home.this, error.toString(), Toast.LENGTH_LONG).show();
                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
      }

答案 4 :(得分:0)

查看我的回答How to use Google/GSON to convert a JSON string into Java POJO?它可以帮助您从响应中创建POJO,而不需要通过密钥手动获取价值。

Gson gson = new Gson();
POJOClass pojo = gson.fromJson(jsonObject.toString(), new TypeToken<POJOClass>() {}.getType());