使用volley获取json数组/对象

时间:2017-12-27 15:27:09

标签: arrays json parsing android-volley

如何使用截击获取以下列表?它没有显示发票的价值。我想访问所有元素。是否有任何层次结构可供使用?我尝试使用层次结构invoice.patient_info。但它有效。如何获取对象和数组?我在使用JSONObjectRequestStringRequest之间感到困惑。请提供解决方案

{
"status": 200,
"invoice": {
    "patient_info": {
        "name": "Aandini",
        "mobile": "0123456789",
        "email": "r@yahoo.com",
        "dob": null,
        "gender": "F"
    },
    "invoice_no": "PSC17ON-2",
    "ref_no": null,
    "cc_code": "3525",
    "age": "12",
    "franchise": "uploads/franchise/Quest_Diagnostics_logo_2015.png",
    "pr_doctor": "self",
    "amount": "680",
    "created_at": "2017-12-26 13:13:50",
    "test_list": [
        {
            "test_code": "395",
            "test_description": "Culture, Urine",
            "test_price": "580"
        },
        {
            "test_code": "5463",
            "test_description": "Urinalysis, Complete",
            "test_price": "100"
        }
    ]
}
}

以下是代码:

public void loadInvoice(final String id){
final String url = "https://diagnostic.hisaabwaale.in/AndroidAppController/getSingleInvoiceInfo?id="+id;
   StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("patient_info");
                    tv.removeAllViewsInLayout();
                    int flag = 1;
                    for (int i =-1; i < jsonArray.length();i++) {
                        TableRow tr = new TableRow(getApplication());
                        tr.setLayoutParams(new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.FILL_PARENT,
                                ViewGroup.LayoutParams.WRAP_CONTENT));
                        if(flag==1) {
                           TextView b6 = new TextView(getApplication());
                            b6.setText("NAME");
                            tr.addView(b6);
                           tv.addView(tr);
                            final View vline = new View(getApplication());
                            vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 10));
                            vline.setBackgroundColor(Color.BLUE);
                            tv.addView(vline);
                            flag = 0;
                        }else{
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            TextView b1 = new TextView(getApplication());
                            String stime1 = jsonObject1.getString("test_code");
                            b1.setText(stime1);
                           tr.addView(b1);
                            tv.addView(tr);
                            final View vline1 = new View(getApplication());
                            vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                            vline1.setBackgroundColor(Color.rgb(1, 132, 143));
                            tv.addView(vline1);
                        }
                    }

                } catch (JSONException e) {
                    progressDialog.dismiss();
                    Toast.makeText(getApplicationContext(),"No Records Found",Toast.LENGTH_LONG);
                    Log.e("Error", "Failed" +e.toString());
                    e.printStackTrace();
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(),"No Records Found", Toast.LENGTH_LONG);
                Log.e("Error", "Try Later" +error.toString());
            }
        });
   };
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}

1 个答案:

答案 0 :(得分:0)

我找到了答案

 JSONObject jsonObject = new JSONObject(response);
                        JSONObject jsonObj = jsonObject.optJSONObject("invoice");
                        String cod = jsonObj.getString("cc_code");
                        cc_code.setText(cod);
JSONObject newObject= jsonObj.getJSONObject("patient_info");
                        String n1 = newObject.getString("name");
                        name.setText(n1);

JSONArray jsonArray = jsonObj.getJSONArray("test_list");