2017-07-16 290 views
0

我可以用這種方式從url解析json,我的json看起來像這樣;Android從url解析json

[ 
    {"rank":1,"title":"The Shawshank Redemption"}, 
    {"rank":2,"title":"The Godfather"}, 
    {"rank":3,"title":"The Godfather: Part II"}, 
    {"rank":4,"title":"The Dark Knight"} 
] 

這是我的android代碼,它的工作原理非常完美;

public class MainActivity extends AppCompatActivity { 

    private ListView listView; 
    private SwipeListAdapter adapter; 
    private List<Movie> movieList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView) findViewById(R.id.listView); 
     movieList = new ArrayList<>(); 
     adapter = new SwipeListAdapter(this, movieList); 
     listView.setAdapter(adapter); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       fetchMovies(); 
      }; 
     }); 
    } 

    private void fetchMovies() { 
     String url = "http://www.url.com/test.json"; 
     JsonArrayRequest req = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         if (response.length() > 0) { 
          for (int i = 0; i < response.length(); i++) { 
           try { 
            JSONObject movieObj = response.getJSONObject(i); 
            int rank = movieObj.getInt("rank"); 
            String title = movieObj.getString("title"); 
            Movie m = new Movie(rank, title); 
            movieList.add(0, m); 
           } catch (JSONException e) { 
           } 
          } 
          adapter.notifyDataSetChanged(); 
         } 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
     MyApplication.getInstance().addToRequestQueue(req); 
    } 
} 

我想解析這個JSON,但我無法解析這個JSON,我不知道。

{ 
    "level":[ 
    { 
     "server":[ 
     {"rank":1,"title":"The Shawshank Redemption"}, 
     {"rank":2,"title":"The Godfather"}, 
     {"rank":3,"title":"The Godfather: Part II"}, 
     {"rank":4,"title":"The Dark Knight"} 
     ] 
    } 
    ] 
} 

我該怎麼做?

謝謝。

+2

使用getJSONArray的級別和服務器...從頂部對象支架總是開始...不要太困難或不同於你有什麼,但你需要一個JSON **對象**請求 –

+0

嗨@ cricket_007你能幫我嗎?我做不到。 – Johny

回答

1

我強烈建議朝着Gson走。

這裏是fetchMovies代碼是你不想做出改變:

private void fetchMovies() { 
    String url = "http://www.url.com/test.json"; 
    JsonObjectRequest req = new JsonObjectRequest(url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        try { 
         JSONArray level = response.getJSONArray("level"); 
         JSONObject item = level.getJSONObject(0); 
         JSONArray server = item.getJSONArray("server"); 
         for (int i = 0; i < server.length(); i++) { 
          try { 
           JSONObject movieObj = server.getJSONObject(i); 
           int rank = movieObj.getInt("rank"); 
           String title = movieObj.getString("title"); 
           Movie m = new Movie(rank, title); 
           movieList.add(0, m); 
          } catch (JSONException e) { 
          } 
         } 
         adapter.notifyDataSetChanged(); 
        } catch (JSONException e) { 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
    MyApplication.getInstance().addToRequestQueue(req); 
} 
+2

Gson不會做網絡請求 –

+1

當然不是,但它比手動解析json更好。你可以按照你想要的方式獲取json。 –

+0

嗨@DiegoMarcher我試過你的代碼,但有一個錯誤。你能檢查圖像嗎?謝謝。 https://image.prntscr.com/image/sJ2yixBfTRCXEBbc5FFZXg.png – Johny