为了账号安全,请及时绑定邮箱和手机立即绑定

如何在模型类中保存内部json数组数据的值并显示在列表中

如何在模型类中保存内部json数组数据的值并显示在列表中

元芳怎么了 2021-08-06 09:59:55
{  "Response": {    "ResponseCode": 1,    "ResponseText": "Success"  },  "Detail": [    {      "DateOfSlot": "08/22/2018",      "AvailableTimeSlots": [        {          "TimeSlotId": 46,          "TimeOfSlot": "01:00PM",          "TimeofSlotDateTime": "2018-08-22T13:00:00"        },        {          "TimeSlotId": 48,          "TimeOfSlot": "01:15PM",          "TimeofSlotDateTime": "2018-08-22T13:15:00"        }      ],      "ScheduleId": 4194,      "GetBlockedTimings": [        {          "DateOfSlot": "8/22/201812:00:00AM",          "AvailableTimeSlots": null,          "ScheduleId": 0,          "GetBlockedTimings": null,          "BlockId": 1822,          "fkTimeId": 43,          "IsDeleted": false,          "fkScheduledId": 4194,          "utcDateOfSlot": "0001-01-01T00:00:00"        }      ],      "BlockId": 0,      "fkTimeId": 0,      "IsDeleted": false,      "fkScheduledId": 0,      "utcDateOfSlot": "2018-08-22T00:00:00Z"    }  ]}我有上面的 json 数组。从这个数组中,我想在列表中显示“timeofslot”参数值。如果有人可以指导我如何在没有 GSON 帮助的情况下通过 json 解析来做到这一点,那将对我有所帮助。
查看完整描述

3 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

您可以简单地解析如下


创建一个模型类作为


public class TimeSlot {


    public String TimeSlotId;

    public String TimeOfSlot;


    public String TimeofSlotDateTime;


    public TimeSlot(String timeSlotId, String timeOfSlot, String timeofSlotDateTime) {

        TimeSlotId = timeSlotId;

        TimeOfSlot = timeOfSlot;

        TimeofSlotDateTime = timeofSlotDateTime;

    }

}

然后将这个类与ArrayList.


 ArrayList<TimeSlot> ovbjTimeSlots = new ArrayList<>();


    try {

        JSONObject jo = new JSONObject(response);

        JSONArray jaDetail = jo.getJSONArray("Detail");

        for (int i = 0; i < jaDetail.length(); i++) {

            JSONArray jsonArray = jaDetail.getJSONObject(i).getJSONArray("AvailableTimeSlots");

            for (int j = 0; j < jsonArray.length(); j++) {

                String TimeSlotId = jsonArray.getJSONObject(i).getString("TimeSlotId");

                String TimeOfSlot = jsonArray.getJSONObject(i).getString("TimeOfSlot");

                String TimeofSlotDateTime = jsonArray.getJSONObject(i).getString("TimeofSlotDateTime");

                ovbjTimeSlots.add(new TimeSlot(TimeSlotId, TimeOfSlot, TimeofSlotDateTime));

            }

        }


    } catch (JSONException e) {

        e.printStackTrace();

    }


查看完整回答
反对 回复 2021-08-06
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

我用jackjson来解析;


private List<String> parseString(String str) throws IOException {

    if(str == null){

        return null;

    }

    List<String> list =new ArrayList<>();

    ObjectMapper mapper = new ObjectMapper();

    JsonNode rootNode = mapper.readTree(str).get("Detail");

    for(int i=0 ; i< rootNode.size() ;i++){

        JsonNode rootnode1 =rootNode.get(i).get("AvailableTimeSlots");

        for(int j =0 ; j<rootnode1.size() ;j++){

            JsonNode rootnode2 =rootnode1.get(j);

            list.add((rootnode2.get("TimeOfSlot")).toString());

        }

    }

    return list;

}


查看完整回答
反对 回复 2021-08-06
?
森栏

TA贡献1810条经验 获得超5个赞

package com.Model;


import com.google.gson.annotations.Expose;

import com.google.gson.annotations.SerializedName;


import java.util.List;


public class Model_Json {


    @SerializedName("Response")

    @Expose

    private Response response;

    @SerializedName("Detail")

    @Expose

    private List<Detail> detail = null;


    public Response getResponse() {

        return response;

    }


    public void setResponse(Response response) {

        this.response = response;

    }


    public List<Detail> getDetail() {

        return detail;

    }


    public void setDetail(List<Detail> detail) {

        this.detail = detail;

    }



    public class Response {


        @SerializedName("ResponseCode")

        @Expose

        private Integer responseCode;

        @SerializedName("ResponseText")

        @Expose

        private String responseText;


        public Integer getResponseCode() {

            return responseCode;

        }


        public void setResponseCode(Integer responseCode) {

            this.responseCode = responseCode;

        }


        public String getResponseText() {

            return responseText;

        }


        public void setResponseText(String responseText) {

            this.responseText = responseText;

        }


    }



    public class GetBlockedTiming {


        @SerializedName("DateOfSlot")

        @Expose

        private String dateOfSlot;

        @SerializedName("AvailableTimeSlots")

        @Expose

        private Object availableTimeSlots;

        @SerializedName("ScheduleId")

        @Expose

        private Integer scheduleId;

        @SerializedName("GetBlockedTimings")

        @Expose

        private Object getBlockedTimings;

        @SerializedName("BlockId")

        @Expose

        private Integer blockId;

        @SerializedName("fkTimeId")

        @Expose

        private Integer fkTimeId;

        @SerializedName("IsDeleted")

        @Expose

        private Boolean isDeleted;

        @SerializedName("fkScheduledId")

        @Expose

        private Integer fkScheduledId;

        @SerializedName("utcDateOfSlot")

        @Expose

        private String utcDateOfSlot;


        public String getDateOfSlot() {

            return dateOfSlot;

        }


        public void setDateOfSlot(String dateOfSlot) {

            this.dateOfSlot = dateOfSlot;

        }


        public Object getAvailableTimeSlots() {

            return availableTimeSlots;

        }


        public void setAvailableTimeSlots(Object availableTimeSlots) {

            this.availableTimeSlots = availableTimeSlots;

        }


        public Integer getScheduleId() {

            return scheduleId;

        }


        public void setScheduleId(Integer scheduleId) {

            this.scheduleId = scheduleId;

        }


        public Object getGetBlockedTimings() {

            return getBlockedTimings;

        }


        public void setGetBlockedTimings(Object getBlockedTimings) {

            this.getBlockedTimings = getBlockedTimings;

        }


        public Integer getBlockId() {

            return blockId;

        }


        public void setBlockId(Integer blockId) {

            this.blockId = blockId;

        }


        public Integer getFkTimeId() {

            return fkTimeId;

        }


        public void setFkTimeId(Integer fkTimeId) {

            this.fkTimeId = fkTimeId;

        }


        public Boolean getIsDeleted() {

            return isDeleted;

        }


        public void setIsDeleted(Boolean isDeleted) {

            this.isDeleted = isDeleted;

        }


        public Integer getFkScheduledId() {

            return fkScheduledId;

        }


        public void setFkScheduledId(Integer fkScheduledId) {

            this.fkScheduledId = fkScheduledId;

        }


        public String getUtcDateOfSlot() {

            return utcDateOfSlot;

        }


        public void setUtcDateOfSlot(String utcDateOfSlot) {

            this.utcDateOfSlot = utcDateOfSlot;

        }


    }





    public class Detail {


        @SerializedName("DateOfSlot")

        @Expose

        private String dateOfSlot;

        @SerializedName("AvailableTimeSlots")

        @Expose

        private List<AvailableTimeSlot> availableTimeSlots = null;

        @SerializedName("ScheduleId")

        @Expose

        private Integer scheduleId;

        @SerializedName("GetBlockedTimings")

        @Expose

        private List<GetBlockedTiming> getBlockedTimings = null;

        @SerializedName("BlockId")

        @Expose

        private Integer blockId;

        @SerializedName("fkTimeId")

        @Expose

        private Integer fkTimeId;

        @SerializedName("IsDeleted")

        @Expose

        private Boolean isDeleted;

        @SerializedName("fkScheduledId")

        @Expose

        private Integer fkScheduledId;

        @SerializedName("utcDateOfSlot")

        @Expose

        private String utcDateOfSlot;


        public String getDateOfSlot() {

            return dateOfSlot;

        }


        public void setDateOfSlot(String dateOfSlot) {

            this.dateOfSlot = dateOfSlot;

        }


        public List<AvailableTimeSlot> getAvailableTimeSlots() {

            return availableTimeSlots;

        }


        public void setAvailableTimeSlots(List<AvailableTimeSlot> availableTimeSlots) {

            this.availableTimeSlots = availableTimeSlots;

        }


        public Integer getScheduleId() {

            return scheduleId;

        }


        public void setScheduleId(Integer scheduleId) {

            this.scheduleId = scheduleId;

        }


        public List<GetBlockedTiming> getGetBlockedTimings() {

            return getBlockedTimings;

        }


        public void setGetBlockedTimings(List<GetBlockedTiming> getBlockedTimings) {

            this.getBlockedTimings = getBlockedTimings;

        }


        public Integer getBlockId() {

            return blockId;

        }


        public void setBlockId(Integer blockId) {

            this.blockId = blockId;

        }


        public Integer getFkTimeId() {

            return fkTimeId;

        }


        public void setFkTimeId(Integer fkTimeId) {

            this.fkTimeId = fkTimeId;

        }


        public Boolean getIsDeleted() {

            return isDeleted;

        }


        public void setIsDeleted(Boolean isDeleted) {

            this.isDeleted = isDeleted;

        }


        public Integer getFkScheduledId() {

            return fkScheduledId;

        }


        public void setFkScheduledId(Integer fkScheduledId) {

            this.fkScheduledId = fkScheduledId;

        }


        public String getUtcDateOfSlot() {

            return utcDateOfSlot;

        }


        public void setUtcDateOfSlot(String utcDateOfSlot) {

            this.utcDateOfSlot = utcDateOfSlot;

        }


    }



    public class AvailableTimeSlot {


        @SerializedName("TimeSlotId")

        @Expose

        private Integer timeSlotId;

        @SerializedName("TimeOfSlot")

        @Expose

        private String timeOfSlot;

        @SerializedName("TimeofSlotDateTime")

        @Expose

        private String timeofSlotDateTime;


        public Integer getTimeSlotId() {

            return timeSlotId;

        }


        public void setTimeSlotId(Integer timeSlotId) {

            this.timeSlotId = timeSlotId;

        }


        public String getTimeOfSlot() {

            return timeOfSlot;

        }


        public void setTimeOfSlot(String timeOfSlot) {

            this.timeOfSlot = timeOfSlot;

        }


        public String getTimeofSlotDateTime() {

            return timeofSlotDateTime;

        }


        public void setTimeofSlotDateTime(String timeofSlotDateTime) {

            this.timeofSlotDateTime = timeofSlotDateTime;

        }


    }




}

实现 'com.google.code.gson:gson:2.2.4'


     List<Model_Json> jsonList= new Gson().fromJson("your_json",new TypeToken<List<Model_Json>>(){}.getType());

     List<Model_Json.AvailableTimeSlot> timeSlots =jsonList.get(0).getDetail().get(0).getAvailableTimeSlots();

这里 timeSlots 是一个列表,您可以使用它来设置适配器。


 ArrayAdapterItem adapter = new ArrayAdapterItem(this, R.layout.list_view_row_item, timeSlots);


  ListView listViewItems = findViewById(R.id.yourId);


  listViewItems.setAdapter(adapter);

// here's our beautiful adapter

public class ArrayAdapterItem extends ArrayAdapter<Model_Json.AvailableTimeSlot> {


    Context mContext;

    int layoutResourceId;

    List<Model_Json.AvailableTimeSlot> data;



    public ArrayAdapterItem(Context mContext, int layoutResourceId, List<Model_Json.AvailableTimeSlot> data) {


        super(mContext, layoutResourceId, data);


        this.layoutResourceId = layoutResourceId;

        this.mContext = mContext;

        this.data = data;

    }


    @Override

    public View getView(int position, View convertView, ViewGroup parent) {



        if(convertView==null){

            // inflate the layout

            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();

            convertView = inflater.inflate(layoutResourceId, parent, false);

        }


        // object item based on the position

        Model_Json.AvailableTimeSlot objectItem = data.get(position);


        // get the TextView and then set the text (item name) and tag (item ID) values

        TextView textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);

        textViewItem.setText(objectItem.getTimeOfSlot());



        return convertView;


    }


}

list_view_row_item.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:padding="10dp" >


    <TextView

        android:id="@+id/textViewItem"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:text="Item name here..."

        android:textSize="15dp" />


</RelativeLayout>


查看完整回答
反对 回复 2021-08-06
  • 3 回答
  • 0 关注
  • 177 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信