0

I can parse json from a url in this way and my json looks like this;

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

This is my android code and it works perfect;

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

I want to parse this json but I couldn't parse this json and I have no idea.

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

How Can I do that?

Thank you.

Johny
  • 45
  • 10
  • 2
    Use getJSONArray for the level and the server... Always start from the top Object bracket... Not too difficult or different from what you have, but you need a json **object** request – OneCricketeer Jul 16 '17 at 02:45
  • Hi @cricket_007 Can you help me please? I couldn't do it. – Johny Jul 16 '17 at 13:00

1 Answers1

3

I would strongly suggest to move towards Gson.

Here is the code for fetchMovies if you don't want to make the change:

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);
}
Diego Marcher
  • 378
  • 3
  • 7