Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

android - How to get an specific element in JSON?

If I have a JSON like below:

{
  "Division": [
      {
        "DivisionId": 1,
        "DivisionName" : "A"
      },
      {
        "DivisionId": 2,
        "DivisionName" : "B"
      }
  ],
 "Title": [
     {
       "TitleId": 11,
       "Title": "Title 1"
     },
     {
       "TitleId": 12,
       "Title": "Title 2"
     }
  ]
}

How can I get the Division only with its values inside? What I'm trying to achieve is to put the values of Division inside my ArrayList. I'm using Volley to get the JSON result and what I tried is on the onResponse I used JSONArray divisionArr = response.getJSONArray("Division"); and loop it here's my code

 JSONArray divisionArr = response.getJSONArray("Division");

                for (int i = 0; i < divisionArr.length(); i++) {
                    Division division = new Division();
                    JSONObject divisionObj = (JSONObject) divisionArr.get(i);

                    division.setId(divisionObj.getInt("DivisionId"));
                    division.setName(divisionObj.getString("DivisionName"));

                    divisionArrayList.add(division);
                }

But I'm having an error ParseError, I maybe doing it wrong, but I don't know what is it. Please help, thank you.

/////// Here's my Volley request

public void getData(Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {

    try{

        String syncCall = Constants.VOLLEY;

        request = new JsonObjectRequest(Method.GET,
                syncCall,
                null,
                listener,
                errorListener);

        request.setRetryPolicy(
                new DefaultRetryPolicy(
                        60000,//DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, // 2500
                        1,//DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 1
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); //1f

        mRequestQueue.add(request);
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

Then in my Activity

  private void callSyncVolley() {

    final ProgressDialog pd = new ProgressDialog(this);
    pd.setMessage("Fetching data....");
    pd.show();

    Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray divisionArr = response.getJSONArray("Division");

                for (int i = 0; i < divisionArr.length(); i++) {
                    Division division = new Division();
                    JSONObject divisionObj = (JSONObject) divisionArr.get(i);

                    division.setId(divisionObj.getInt("DivisionId"));
                    division.setName(divisionObj.getString("DivisionName"));

                    divisionArrayList.add(division);
                }

                pd.dismiss();

            } catch (JSONException e) {
                e.printStackTrace();
                Log.e(TAG, "Error: " + e.getMessage());
                pd.dismiss();
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            if (error.networkResponse != null) {
                Log.d(TAG, "Error Response code: " + error.networkResponse.statusCode);
                pd.dismiss();
            }
            if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                Log.d(TAG, "Error Response code: Timeout/NoConnection");
                pd.dismiss();
            } else if (error instanceof AuthFailureError) {
                //TODO
                Log.d(TAG, "Error Response code: AuthFailureError");
                pd.dismiss();
            } else if (error instanceof ServerError) {
                //TODO
                Log.d(TAG, "Error Response code: ServerError");
                pd.dismiss();
            } else if (error instanceof NetworkError) {
                //TODO
                Log.d(TAG, "Error Response code: NetworkError");
                pd.dismiss();
            } else if (error instanceof ParseError) {
                //TODO
                Log.d(TAG, "Error Response code: ParseError");
                pd.dismiss();
            }
        }
    };

    VolleyRequestManager.getInstance().doRequest().getData(listener, errorListener);
}

The error only shows Error Response code: ParseError

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your JSON format is invalid,

{
  "Division": [
      {
        "DivisionId": 1,
        "DivisionName" : A
      },
      {
        "DivisionId": 2,
        "DivisionName" : B
      }
  ],
 "Title": [
     {
       "TitleId": 11,
       "Title": "Title 1"
     },
     {
       "TitleId": 12,
       "Title": "Title 2"
     }
  ],
}

I just pasted your format here

  1. divisionArr.setName(divisionObj.getString("DivisionName")); &&

You are trying to access a String which is not wrapped in double quotes,the String A and String B is not wrapped in double quotes.

  1. Unnecessary comma at the end of the array ],

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...