简体   繁体   中英

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:

[
    {
        "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. Can anyone have a solution? 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. Can anyone have a solution? 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.

Solution:

Instead of initializing dataMessage inside the loop, you just need to keep on adding the latest message that you find in each iteration:

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

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

Create a POJO representing your Json structure. Use a serialization/deserialization library such as GSON to get the object from json load.

for your response, A POJO mode similar to this would help.

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/ .

Just change the = with add as follows :

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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