2017-12-27 218 views
0

如何使用抽球獲取下面的列表?它沒有顯示發票的價值。我想訪問所有的元素。是否有任何層次結構可供使用?我嘗試使用層次結構invoice.patient_info。但它的功效。如何獲取對象和數組?林混淆了使用JSONObjectRequestStringRequest。請提供一個解決方案使用抽球獲取json數組/對象

{ 
"status": 200, 
"invoice": { 
    "patient_info": { 
     "name": "Aandini", 
     "mobile": "", 
     "email": "[email protected]", 
     "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); 
} 

回答

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