使用抽球获取json数组/对象
如何使用抽球获取下面的列表?它没有显示发票的价值。我想访问所有的元素。是否有任何层次结构可供使用?我尝试使用层次结构invoice.patient_info
。但它的功效。如何获取对象和数组?林混淆了使用JSONObjectRequest
和StringRequest
。请提供一个解决方案使用抽球获取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);
}
回答:
我找到了答案
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");
以上是 使用抽球获取json数组/对象 的全部内容, 来源链接: utcz.com/qa/266035.html