我正在尝试解析对象嵌套的json对象。我收到了错误

时间:2017-07-18 05:25:34

标签: java android

请帮我理解我哪里出错了。!!我想解析json     仅对象。但无法弄明白。 json由嵌套对象组成。

JSON格式

{"-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"},

json结构由嵌套对象组成。并且没有      数组结构。 **    ** 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.!!

1 个答案:

答案 0 :(得分:0)

解析JsonObject尝试下面这样..,

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();
                }