简体   繁体   English

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

[英]How to parse the JSON response coming from server which is in this format

I have a JSON response coming from the server which looks like: 我有一个来自服务器的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"
            }
        ]
    }
]

I want to know how to parse this data, how can I send this data to my adapter, I've tried using: 我想知道如何解析这些数据,如何将这些数据发送到适配器,我尝试使用以下方法:

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

But it's only passing the last message I mean dataMessage is overriding with the latest coming message but I want all the messages in dataMessage. 但这只是传递最后一条消息,我的意思是dataMessage被最新的消息覆盖,但我希望dataMessage中的所有消息。 Can anyone have a solution? 谁能找到解决方案? TIA TIA

But it's only passing the last message I mean dataMessage is overriding with the latest coming message but I want all the messages in dataMessage. 但这只是传递最后一条消息,我的意思是dataMessage被最新的消息覆盖,但我希望dataMessage中的所有消息。 Can anyone have a solution? 谁能找到解决方案? TIA TIA

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

Problem: 问题:

=> Because you are iterating the loop and storing value in the dataMessage variable and so obviously at the end of the loop iteration dataMessage would be having the last message value. =>因为您要迭代循环并将值存储在dataMessage变量中,所以显然在循环结束时, dataMessage将具有最后一个消息值。

Solution: 解:

Instead of initializing dataMessage inside the loop, you just need to keep on adding the latest message that you find in each iteration: 无需在循环内初始化dataMessage,只需继续添加每次迭代中找到的最新消息:

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

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

Create a POJO representing your Json structure. 创建一个代表您的Json结构的POJO。 Use a serialization/deserialization library such as GSON to get the object from json load. 使用序列化/反序列化库(例如GSON)从json加载中获取对象。

for your response, A POJO mode similar to this would help. 对于您的响应,类似于此的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;
}

}

It is converted using http://www.jsonschema2pojo.org/ . 使用http://www.jsonschema2pojo.org/进行了转换。

Just change the = with add as follows : 只需使用add更改= ,如下所示:

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

Also you can use 你也可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM