0

Please help me understand where i'm going wrong.!! I want to parse the json objects only. But couldn't able to figure it out. The json consists of nested objects.

JSON Format

{"-3fsdfsdfsfsd3":{"abbreviation":"CC","level":"High 
  School","name":"Catholic 
  Central","picUrl":"https://firebasestorage.googleapis.com/v0/b/minihm-
  584e8.appspot.com/o/cc.png?alt=media&token=2ff31f9e-90cf-45a8-b39c-
  00c2ae932cba","uuid":"-3fsdfsdfsfsd3"},"-Kbhx1WaxclQwu56Hrb1":
{"abbreviation":"DC","level":"High School","name":"Divine 
  Child","picUrl":"https://firebasestorage.googleapis.com/v0/b/minihm-
  584e8.appspot.com/o/dc.png?alt=media&token=14b4ebad-c017-46b5-89ec-
  86e4c0d0edd3","uuid":"-Kbhx1WaxclQwu56Hrb1"},

The json structure consists of nested objects. and has no array structure. ** **Mainactivity

public class MainActivity extends Activity {
**// Log tag**
private static final String TAG = MainActivity.class.getSimpleName();
private static final String url =**json url goes here**
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);


     //Showing progress dialog before making http request

    pDialog.setMessage("Loading...");
    pDialog.show();
    **// Creating volley request obj**
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();


      // Parsing json ( Edited with the previous post )
                        try {
                            JSONObject jsonObject = new JSONObject(response.toString());

                            JSONObject jsonObject1 = jsonObject.getJSONObject("-3fsdfsdfsfsd3");
                            Movie movie = new Movie();
                            movie.setName(jsonObject1.getString("name"));
                            movie.setThumbnailUrl(jsonObject1.getString("picUrl"));
                            movie.setLevel(jsonObject1.getString("level"));

                            movieList.add(movie);

                        } 


 // adding content to model array



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }


   // notifying list adapter about data changes
                    // so that it renders the list view with updated data**

                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });



    // Adding request to request queue

    AppController.getInstance().addToRequestQueue(movieReq);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}
private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}


**LOGCAT ERRORS:**
07-19 22:29:50.321 4921-4921/com.manojgudihal.example D/Volley: [1] 
2.onErrorResponse: MainActivity

Please help.!!
Manoj
  • 17
  • 3

1 Answers1

0

Parsing JsonObject try below like this..,

try {
                    JSONObject jsonObject = new JSONObject(response.toString());

                    JSONObject jsonObject1 = jsonObject.getJSONObject("3fsdfsdfsfsd3");
                    Movie movie = new Movie();
                    movie.setName(jsonObject1.getString("name"));
                    movie.setThumbnailUrl(jsonObject1.getString("picUrl"));
                    movie.setLevel(jsonObject1.getString("level"));

                    movieList.add(movie);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
piet.t
  • 11,718
  • 21
  • 43
  • 52
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • Hi, thanks for the solution. what to include in the json object if there is a continuation of json structure. – Manoj Jul 18 '17 at 06:12
  • @Manoj can You explain me more on this – Gowthaman M Jul 18 '17 at 06:15
  • The Json structure consists of nested objects. – Manoj Jul 18 '17 at 06:22
  • JSONObject jsonObject2 = jsonObject.getJSONObject("Change yourkey"); else if you want to use for loop then you have to use JsonArray....Then your response should be something like this example...[ "-3fsdfsdfsfsd3": { "abbreviation": "CC", "level": "High School", "name": "Catholic Central", "picUrl": "https://firebasestorage.googleapis.com/v0/b/minihm- 584e8.appspot.com/o/cc.png?alt=media&token=2ff31f9e-90cf-45a8-b39c- 00c2ae932cba", "uuid": "-3fsdfsdfsfsd3" }, { "level": "High School", }, { "level": "High School", } ] – Gowthaman M Jul 18 '17 at 06:47
  • Hi Gowthaman. I tried to implement the code i got the above errors. to which i was not able to figure it out. Please help I have added the Logcat errors to which i'm facing. – Manoj Jul 18 '17 at 17:24
  • Hi Manoj,You are using the same above responce or what? – Gowthaman M Jul 19 '17 at 02:42
  • Hi Gowthaman, yes i've changed my code to the above response that you gave. – Manoj Jul 19 '17 at 02:58
  • JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener() {@Override public void onResponse(JSONArray response) {Log.d(TAG, response.toString());hidePDialog();try {JSONObject jsonObject = new JSONObject(response.toString());JSONObject jsonObject1 = jsonObject.getJSONObject("-3fsdfsdfsfsd3");Movie movie = new Movie(); movie.setName(jsonObject1.getString("name")); movie.setThumbnailUrl(jsonObject1.getString("picUrl"));movie.setLevel(jsonObject1.getString("level"));movieList.add(movie);} – Manoj Jul 20 '17 at 02:36
  • Hi Gowthaman, sure bro i will check with them and try to explain the json structure and will get back with you. – Manoj Jul 20 '17 at 06:20