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
  • 18,460
  • 17
  • 76
  • 134
Arun Sriramula
  • 124
  • 1
  • 13
  • response is coming in JSONArray. – Abhinav Gupta Aug 21 '19 at 07:03
  • Are you using `Retrofit` can you please post your Model and your ApiService? And how do you call it? – Skizo-ozᴉʞS Aug 21 '19 at 07:04
  • Use GSON library to parsing Json https://github.com/google/gson – Anas Mehar Aug 21 '19 at 07:05
  • @AbhinavGupta Yes it's coming in JSONArray and I have a different POJO class for that too which consists of `private String updated_at; private String user_id; private String created_at; private LatestMessage[] latestMessage; private String ticket_ref_no; private String status;` this things getter and setter methods – Arun Sriramula Aug 21 '19 at 07:05
  • Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Manohar Aug 21 '19 at 07:05
  • @Skizo-ozᴉʞS I've created two model classes one is what I mentioned in the comment already which is the root and the other is for LatestMessage which consists of getter and setters of `private String updated_at; private String user_id; private String created_at; private String response_by_user_id; private String message; private String ticket_id;` – Arun Sriramula Aug 21 '19 at 07:08
  • iterate through the top list of objects and add all their `latestMessage` to the list. Then use that list. – Vladyslav Matviienko Aug 21 '19 at 07:08
  • @ArunSriramula Could you post how do you get these information? please – Skizo-ozᴉʞS Aug 21 '19 at 07:13
  • @Skizo-ozᴉʞS which information? – Arun Sriramula Aug 21 '19 at 07:17
  • @ArunSriramula check my answer and let me know if it work – Skizo-ozᴉʞS Aug 21 '19 at 07:30

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
  • 125,853
  • 70
  • 238
  • 294
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/.

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
  • 18,460
  • 17
  • 76
  • 134
  • 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