如何使用 Volley 解析嵌套的 JSON? [英] How to Parse nested JSON using Volley?

查看:33
本文介绍了如何使用 Volley 解析嵌套的 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


    }

这就是我所做的

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

推荐答案

试试这个.

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

这篇关于如何使用 Volley 解析嵌套的 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆