如何解析这种格式的来自服务器的JSON响应

时间:2019-08-21 07:01:19

标签: android arrays json parsing retrofit2

我有一个来自服务器的JSON响应,如下所示:

[
    {
        "user_id": 147,
        "ticket_ref_no": "6ef8b3be-b3b7-4ffb-b8ca-6f114d972553",
        "status": "open",
        "created_at": "2019-08-20 17:08:29",
        "updated_at": "2019-08-20 17:08:29",
        "latestMessage": [
            {
                "message": "Created New Ticket for test",
                "ticket_id": 2,
                "user_id": 147,
                "response_by_user_id": null,
                "created_at": "2019-08-20 17:08:29",
                "updated_at": "2019-08-20 17:08:29"
            }
        ]
    },
    {
        "user_id": 147,
        "ticket_ref_no": "d1c022f2-c12b-45ed-8d74-befc4896c5e2",
        "status": "open",
        "created_at": "2019-08-20 17:22:14",
        "updated_at": "2019-08-20 17:22:14",
        "latestMessage": [
            {
                "message": "Help Test",
                "ticket_id": 3,
                "user_id": 147,
                "response_by_user_id": null,
                "created_at": "2019-08-20 17:22:14",
                "updated_at": "2019-08-20 17:22:14"
            }
        ]
    }
]

我想知道如何解析这些数据,如何将这些数据发送到我的适配器,我尝试使用:

for (int i = 0; i<data.size(); i++)
   dataMessage = new ArrayList<>(Arrays.asList(data.get(i).getLatestMessage()));

但这只是传递最后一条消息,我的意思是dataMessage被最新的消息覆盖,但我希望dataMessage中的所有消息。 谁能找到解决方案? TIA

4 个答案:

答案 0 :(得分:2)

但这只是传递最后一条消息,我的意思是dataMessage被最新的消息覆盖,但我希望dataMessage中的所有消息。谁能找到解决方案? TIA

for (int i = 0; i<data.size(); i++)
    dataMessage = new ArrayList<>(Arrays.asList(data.get(i).getLatestMessage()));

问题:

=>因为您要迭代循环并将值存储在dataMessage变量中,所以显然在循环迭代结束时dataMessage将具有最后一个消息值。

解决方案:

您无需继续在循环内初始化dataMessage,而只需继续添加在每次迭代中找到的最新消息:

 for (int i = 0; i<data.size(); i++)
        dataMessage.add(data.get(i).getLatestMessage());

答案 1 :(得分:0)

创建一个模型类,设置从json获取的所有值,并将该模型类对象添加到arrarylist并将该arraylist发送到适配器

答案 2 :(得分:0)

创建一个代表您的Json结构的POJO。使用序列化/反序列化库(例如GSON)从json加载中获取对象。

对于您的响应,类似于此的POJO模式会有所帮助。

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("user_id")
@Expose
private Integer userId;
@SerializedName("ticket_ref_no")
@Expose
private String ticketRefNo;
@SerializedName("status")
@Expose
private String status;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("latestMessage")
@Expose
private List<LatestMessage> latestMessage = null;

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public String getTicketRefNo() {
return ticketRefNo;
}

public void setTicketRefNo(String ticketRefNo) {
this.ticketRefNo = ticketRefNo;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public String getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}

public List<LatestMessage> getLatestMessage() {
return latestMessage;
}

public void setLatestMessage(List<LatestMessage> latestMessage) {
this.latestMessage = latestMessage;
}

}
-----------------------------------com.example.LatestMessage.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class LatestMessage {

@SerializedName("message")
@Expose
private String message;
@SerializedName("ticket_id")
@Expose
private Integer ticketId;
@SerializedName("user_id")
@Expose
private Integer userId;
@SerializedName("response_by_user_id")
@Expose
private Object responseByUserId;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Integer getTicketId() {
return ticketId;
}

public void setTicketId(Integer ticketId) {
this.ticketId = ticketId;
}

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public Object getResponseByUserId() {
return responseByUserId;
}

public void setResponseByUserId(Object responseByUserId) {
this.responseByUserId = responseByUserId;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public String getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}

}

使用http://www.jsonschema2pojo.org/进行转换。

答案 3 :(得分:0)

只需将=更改为add如下:

for (int i = 0; i<data.size(); i++)
        dataMessage.add(Arrays.asList(data.get(i).getLatestMessage());

还可以使用

Collections.addAll(dataMessage, data.get(i).getLatestMessage());