0

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

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Arun Sriramula
  • 124
  • 1
  • 13

4 Answers4

2

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());
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

Create one model class,set all values which are getting from json and add that model class object to arrarylist and send that arraylist to adapter

0

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

shubham vashisht
  • 255
  • 2
  • 11
0

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());
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Great you gave me an hint. Thank You @Skizo-ozᴉʞS – Arun Sriramula Aug 21 '19 at 07:37
  • I just did this `dataMessage.addAll(Arrays.asList(data.get(i).getLatestMessage()));` and it worked – Arun Sriramula Aug 21 '19 at 07:43
  • I know what matters most is the problem got resolved. However, we should always look at the problem and the options to solve the problem, and we should think upon each solution. I'm wondering how it gave you hint where as mine answer not! – Paresh Mayani Aug 21 '19 at 07:49
  • @PareshMayani I don't know how but it just clicked when I saw this answer, maybe because in your answer there weren't `Arrays.asList` keyword – Arun Sriramula Aug 21 '19 at 07:51
  • @PareshMayani I figured it out reading the comments and reading all of your answers, sometimes is better to understand the problem as you say instead of typing whatever without knowing what's the root cause, i figured it out when he said : cause my root POJO has LatestMessage[] type element and dataMessage have a type of List without square brackets – Skizo-ozᴉʞS ツ Aug 21 '19 at 07:51
  • @Skizo-ozᴉʞS but we should always be suggesting best practices. addAll() method is used to all many elements in the collection at a time but here, in this case, it's being added one by one through iteration. – Paresh Mayani Aug 21 '19 at 07:55