Johny Johny - 4 months ago 18
Android Question

Android parse json from a url

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.

Answer Source

I would strongly suggest to move towards Gson.

Here is the code for fetchMovies is 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);
}