JSON响应:
{"schedulePlan":[{"slot_id":1,"slot":"08:00 AM - 09:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"33","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"}]},{"slot_id":2,"slot":"09:00 AM - 10:00 AM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":3,"slot":"10:00 AM - 11:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"22","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"},{"booking_id":55,"patientName":"Om","patientAddress":"Sector 33, Noida"}]},{"slot_id":4,"slot":"11:00 AM - 12:00 PM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":5,"slot":"12:00 PM - 01:00 PM","slotStatus":"booked","booking_details":[{"booking_id":"76","patientName":"Raj","patientAddress":"Sector 83, Noida"}]}]}
需要输出:
患者姓名:Malik
患者姓名:
患者姓名:Malik,Om
患者姓名:
患者姓名:Raj
地址也一样。
在每个列表项上单击,需要显示患者姓名。 如何解析响应并显示上述的patientName和patientAddress?
型号: 模型1:
public class RescheduleModel {
private String slot_id;
private String slot;
private String slotStatus;
public ArrayList<BookingDetailsModel> bookingDetailsModel = new ArrayList<BookingDetailsModel>();
public String getSlot_id() {
return slot_id;
}
public void setSlot_id(String slot_id) {
this.slot_id = slot_id;
}
public String getSlot() {
return slot;
}
public void setSlot(String availableSlot) {
this.slot = availableSlot;
}
public String getSlotStatus() {
return slotStatus;
}
public void setSlotStatus(String slotStatus) {
this.slotStatus = slotStatus;
}
public List<BookingDetailsModel> getBookingDetailsModels() {
return bookingDetailsModel;
}
public void setBookingDetailsModel(ArrayList<BookingDetailsModel> bookingDetailsModel) {
this.bookingDetailsModel = bookingDetailsModel;
}
模型2:
public class BookingDetailsModel {
private String booking_id;
private String patientName;
private String patientAddress;
public String getBooking_id() {
return booking_id;
}
public void setBooking_id(String booking_id) {
this.booking_id = booking_id;
}
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPatientAddress() {
return patientAddress;
}
public void setPatientAddress(String patientAddress) {
this.patientAddress = patientAddress;
}
活动:
public void parseJsonResponse(String result) {
Log.i(TAG, result);
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = new JSONArray(json.getString("schedulePlan"));
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
RescheduleModel rescheduleModel = new RescheduleModel();
rescheduleModel.setSlot_id(jObject.getString("slot_id"));
rescheduleModel.setSlot(jObject.getString("slot"));
rescheduleModel.setSlotStatus(jObject.getString("slotStatus"));
JSONArray array = jObject.getJSONArray("booking_details");
for (int j = 0; j < array.length(); j++) {
JSONObject object = array.getJSONObject(j);
BookingDetailsModel model = new BookingDetailsModel();
model.setPatientName(object.getString("patientName"));
model.setPatientAddress(object.getString("patientAddress"));
Log.i("PNAME...", object.getString("patientName"));
Log.i("PADDRESS...", object.getString("patientAddress"));
bookingDetails.add(model);
}
slots.add(rescheduleModel);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
输出:
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Malik
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: D-77, Sector 63,
Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Malik
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: D-77, Sector 63,
Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Om
04-21 15:17:04.431 28588-28588/com.example I/PADDRESS...: Sector 33, Noida
04-21 15:17:04.431 28588-28588/com.example I/PNAME...: Raj
04-21 15:17:04.432 28588-28588/com.example I/PADDRESS...: Sector 83, Noida
当我点击列表视图中的第三行时,我想显示“Malik,Om”。 目前,Malik和Om正被添加到不同的行中。
OnClick:
nextDetail.setText("Next slot is booked for '" + bookingDetails.get(position + 1).getPatientName() + "' at '" +
bookingDetails.get(position + 1).getPatientAddress() + "'.");
答案 0 :(得分:1)
以下是工作代码:
public void parseJsonResponse(String result) {
Log.i("JSON", result);
try {
JSONObject json = new JSONObject(result);
JSONArray jArray = new JSONArray(json.getString("schedulePlan"));
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
JSONArray array = jObject.getJSONArray("booking_details");
StringBuilder stringBuilderName = new StringBuilder();
StringBuilder stringBuilderAddress = new StringBuilder();
for (int j = 0; j < array.length(); j++) {
JSONObject object = array.getJSONObject(j);
String name = object.getString("patientName");
String address = object.getString("patientAddress");
if (TextUtils.isEmpty(name))
stringBuilderName.append(" ");
else
stringBuilderName.append(name);
if (TextUtils.isEmpty(address))
stringBuilderAddress.append(" ");
else
stringBuilderAddress.append(address);
if (j < array.length() - 1) {
stringBuilderName.append(", ");
stringBuilderAddress.append(", ");
}
}
Log.i("PNAME...", stringBuilderName.toString());
Log.i("PADDRESS...", stringBuilderAddress.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
<强>输出:强>
04-21 16:34:37.842 8171-8171/com.ferdous.myapplication5 I/JSON: {"schedulePlan":[{"slot_id":1,"slot":"08:00 AM - 09:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"33","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"}]},{"slot_id":2,"slot":"09:00 AM - 10:00 AM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":3,"slot":"10:00 AM - 11:00 AM","slotStatus":"booked","booking_details":[{"booking_id":"22","patientName":"Malik","patientAddress":"D-77, Sector 63, Noida"},{"booking_id":55,"patientName":"Om","patientAddress":"Sector 33, Noida"}]},{"slot_id":4,"slot":"11:00 AM - 12:00 PM","slotStatus":"available","booking_details":[{"booking_id":"","patientName":"","patientAddress":""}]},{"slot_id":5,"slot":"12:00 PM - 01:00 PM","slotStatus":"booked","booking_details":[{"booking_id":"76","patientName":"Raj","patientAddress":"Sector 83, Noida"}]}]}
04-21 16:34:37.842 8171-8171/com.ferdous.myapplication5 I/PNAME...: Malik
04-21 16:34:37.842 8171-8171/com.ferdous.myapplication5 I/PADDRESS...: D-77, Sector 63, Noida
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...:
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...:
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...: Malik, Om
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...: D-77, Sector 63, Noida, Sector 33, Noida
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...:
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...:
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PNAME...: Raj
04-21 16:34:37.852 8171-8171/com.ferdous.myapplication5 I/PADDRESS...: Sector 83, Noida
答案 1 :(得分:0)
您可以执行以下代码
//jsonObject is your jsonData which you get From Server
try {
JSONArray array = jsonObject.getJSONArray("schedulePlan");
for (int i = 0; i < array.length(); i++) {
JSONObject data = array.getJSONObject(i);
int SlotId = data.getInt("slot_id");
String slot = data.getString("slot");
String slotStatus = data.getString("slotStatus");
JSONArray booking_details = data.getJSONArray("booking_details");
for (int j = 0; j < booking_details.length(); j++) {
JSONObject bookingData = booking_details.getJSONObject(i);
String booking_id = bookingData.getString("booking_id");
String patientName = bookingData.getString("patientName");
String patientAddress = bookingData.getString("patientAddress");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
像这样你可以解析并制作POJO类或存储变量,并且可以在任何你想要的地方使用
答案 2 :(得分:0)
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = null;
jsonArray = jsonObject.getJSONArray("schedulePlan");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONObject jsonObject2 = jsonObject1.getJSONArray("booking_details").getJSONObject(0);
jsonObject2.getString("patient_name");
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 3 :(得分:0)
@Inject
public DataManager(@Named(FIREBASE) Retrofit firebaseRetrofit,
@Named(ADS) Retrofit adsRetrofit,
... ) {
...
}
答案 4 :(得分:0)
我认为你正在使用凌空,然后试试这个:
class Patient{
private String name,address
Patient(String name,String address)
{
this.name=name;
this.address=address;
}
}
class SampleClass{
//create a arrayList of patient
ArrayList<String> patientArraryList= new ArrayList<>();
/*
your code
*/
//in your on response section use this code
public void onResponse(JSONObject response)
{
JSONArray jarray=response.getJSONArray("schedulePlan");
for(int i=0;i<jarray.length();i++){
JSONObject temp= jarray.getJSONObject(i);
JSONArray jarray2=temp.getJSONArray("booking_details");
for(int j=0;j<=jarray.length();j++){
patientArraryList.add(
new Patient(
jarray2.getJSONObject(j).getString("patientName"),
jarray2.getJSONObject(j).getString("patientAddress")));
}
}
}
//now your data is ready and is in patientArraryList
//use adapter to show in list, you can find many examples of //adapter
答案 5 :(得分:0)
将您的JSON字符串here并在网址中选择以下选项:
来源类型:JSON
注释风格:Gson
它将使用json字符串创建模型类,并在您的应用程序中添加所有创建的类</ p>
然后在构建gradle中添加此gradle compile 'com.google.code.gson:gson:2.7'
并尝试此Example example = new Gson().fromJson("Your Json String",Example.class);
答案 6 :(得分:-1)
如果你签入你所拥有的json数据,对于第三项,你有两个预订细节,所以当你点击recyclerview第3项时,首先需要通过使用for循环将这两个父名添加到一个字符串中,然后设置结果数据到textView。
希望这有用..
@Rajesh