2017-07-18 82 views
0

請幫我理解我要去哪裏錯了。我只想解析json 對象。但無法弄清楚。 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.!! 
+2

和什麼是錯的,你在做什麼? –

+0

您錯過了一個名爲「-3fsdfsdfsfsd3」的對象。應該設置該對象的值。 –

回答

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

嗨,感謝您的解決方案。如果有json結構的延續,那麼在json對象中包含什麼。 – Manoj

+0

@Manoj你能解釋一下嗎? –

+0

Json結構由嵌套對象組成。 – Manoj