Mohammad Taqi
Mohammad Taqi

Reputation: 179

How to get value from this type of json array in android studio?

[{"error":"no error"}]
[{"uid":"20","uname":"Velani Hasnain Raza","uage":"13","umo":"98658912","city":"jhguva","state":"Gffhat",
"country":"Ingja","pass":"000000gg0","image":""}]
[{"rank":"NA","total":"6","score":"3","played":"2"}]

.......my code.....

JsonArrayRequest request = new JsonArrayRequest(WebServiceUrl, new Response.Listener() { @Override public void onResponse(JSONArray response) { pd1.dismiss(); MyLog.p(response.toString());

            try {

                JSONArray json=new JSONArray(response);
                JSONArray array =json.getJSONArray(0);
                JSONArray array1 =json.getJSONArray(1);
                JSONArray array2 =json.getJSONArray(2);
                String error = array.getJSONObject(0).getString("error");
                if (error.equals("no error") == false)
                    Common.ShowError(getContext(), error);
                else {
                   JSONObject current = array1.getJSONObject(0);

                    name = current.getString("uname");
                    pro_userName.setText(name);

                    city = current.getString("city");
                    state = current.getString("state");
                    pro_cityState.setText(city + "," + state);

                    played = current.getString("played");
                    totalQuiz.setText(played);

                    total_score = current.getString("total");
                    user_score = current.getString("score");
                    totalMarks.setText(user_score+"/"+total_score);

                    JSONObject current2 = array2.getJSONObject(0);
                        rank = current2.getString("rank");
                    overallRank.setText(rank);

                }
            } catch (JSONException e) {
                Common.ShowError(getContext(), e.getMessage());
                MyLog.p(e.getMessage()+"\n catcj block");

}

Upvotes: 0

Views: 114

Answers (1)

brew
brew

Reputation: 103

First, you need to implement Gson Dependencies

dependencies {
    implementation 'com.google.code.gson:gson:2.8.6'
}

Then retrieve the Json into a custom object

ArrayList< /*your object type*/ >listError = new ArrayList<>();

Gson gson = new Gson();
try {
    JSONArray jsonArray = new JSONArray(response);
    for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject jsonObject = jsonArray.getJSONObject(i);
     switch(i) {
       case 0:
          /*your custom object*/ = gson.fromJson(jsonObject.toString(), /* your Class */.class);
          listError.add(/*your custom object*/);
       break;
       case 1:
         /*
          * your next object 
          * .... and so on 
          * create different type and use gson to parse them
          */
        break;
       }      
     }
   } catch (JSONException e) {
       e.printStackTrace();
}

This should work. make sure that your model class has the same name as of JSON parameters and datatype. it will parse the JSON array to type List of java

Upvotes: 0

Related Questions