mDeveloper
mDeveloper

Reputation: 122

How to Parse nested JSON using Volley?

MainClass.java

RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;
    private Toolbar mToolbar;
    private List<Onwardflights> onwardflights;
    private static final String url = "http://tripofareapi.herokuapp.com/flightapi/src=DEL&dest=BOM&depdate=20180420&arrdate=20180422"; // https: url will  insert here
    ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flight_suggestion);
        mToolbar = (Toolbar) findViewById(R.id.flight_suggestion_appbar);

        Bundle bundle = getIntent().getExtras();
        String from = bundle.getString("from");
        String to = bundle.getString("to");

        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle(from + "-" + to);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mToolbar.setTitleTextColor(Color.WHITE);

        recyclerView = (RecyclerView) findViewById(R.id.flight_suggestion_recyclerview);
        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        onwardflights = new ArrayList<>();
        getdata();


    }

This is what i have done

    private void getdata() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait, while we load the data");
        progressDialog.show();

       //*Could not understand how to parse FARE objects*
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jo = jsonArray.getJSONObject(i);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(request);
    }

Upvotes: 0

Views: 1952

Answers (2)

Abu Yousuf
Abu Yousuf

Reputation: 6107

First check your response format in Json formatter and validator.

Your json response has 2 json object

  1. data
  2. data_length

to get JSONObject form a Json user getJSONObject("key") and to get JSONArray from Json use getJSONArray("key").

As node data is a JSONObject use getJSONObject("key")` to get that.

    JSONObject jsonObject = new JSONObject(response);
    JSONObject data = jsonObject.getJSONObject("data");

    JSONArray  jsArray1 = data.getJSONArray("returnflights");
    JSONArray  jsArray2 = data.getJSONArray("onwardflights");

    for (int i = 0; i < jsArray1.length(); i++) {
        JSONObject jo = jsArray1.getJSONObject(i);

    }

    ..................

Check this tutorial to learn how to parse Json

Upvotes: 0

Nouman Ch
Nouman Ch

Reputation: 4121

Try this.

 private void getdata() {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please wait, while we load the data");
        progressDialog.show();

       //*Could not understand how to parse FARE objects*
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject data = jsonObject.getJSONObject("data");
                    JSONArray  returnflights = data.getJSONArray("returnflights");
                 JSONArray  onwardflights = data.getJSONArray("onwardflights");
                    for (int i = 0; i < returnflights.length(); i++) {
                        JSONObject returnFlightChild = returnflights.getJSONObject(i);
                     //returnFlights objects
                    }
                   for (int i = 0; i < onwardflights.length(); i++) {
                        JSONObject onwardFlightsChild = onwardflights.getJSONObject(i);
                   //onwardFlight objects
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(request);
    }

Upvotes: 1

Related Questions