0

I have the following JSON where the parameter "-LCxUVIOiLT4TUM4Os3U" and "-LCxUVMI85eea0kISlc5" is a dynamic parameter, ie it is never the same parameter name (it is an ID of the Firebase Realtime Database database)

{

  "compliant" : {
    "emails" : {
      "-LCxUVIOiLT4TUM4Os3U" : {
        "date" : "2018-05-19T20:35:23.483Z",
        "email" : "test@hotmail.com",
        "messageId" : "d39126a2-29b0-5702-b0a5-75d8a57b0a1d",
        "notificationType" : "Complaint"
      },
      "-LCxUVMI85eea0kISlc5" : {
        "date" : "2018-05-19T12:00:10.527Z",
        "email" : "test2@hotmail.com",
        "messageId" : "5b2ee1af-87c0-5aa3-ace3-b2d593ca7cb3",
        "notificationType" : "Complaint"
      }
    }
  }
}

How can I manipulate this parameter and deserialize it in a JAVA Object?

I have done several tests, but none with a positive result.

Note: I'm using Gson.

Updated Class:

package tv.zoome.integration.data;

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

public class Email {

@SerializedName("date")
@Expose
private String date;
@SerializedName("email")
@Expose
private String email;
@SerializedName("messageId")
@Expose
private String messageId;
@SerializedName("notificationType")
@Expose
private String notificationType;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getMessageId() {
return messageId;
}

public void setMessageId(String messageId) {
this.messageId = messageId;
}

public String getNotificationType() {
return notificationType;
}

public void setNotificationType(String notificationType) {
this.notificationType = notificationType;
}

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vitor Darela
  • 142
  • 7
  • Should work if the `emails` field is of type `Map` (where Object could be a specific class if available) – ernest_k May 20 '18 at 12:46
  • Hello, you write better how should I do? Do you have any sample links that I can follow? – Vitor Darela May 20 '18 at 12:53
  • Please add the class into which this has to be deserialized (the one you already have), and we'll suggest a version that will support your dynamic fields. – ernest_k May 20 '18 at 12:55
  • I updated the issue with my object class. Thank you for your help – Vitor Darela May 20 '18 at 13:01
  • 1
    Right. Just declare emails as `private Map emails`. Take a look at this answer: https://stackoverflow.com/a/6796987/5761558 – ernest_k May 20 '18 at 13:11
  • @ErnestKiwele From a look at the code below, when I try to get the attribute I get the following cast error: Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to zoome.integration.tv.data.Email ObjectMapper mapper = new ObjectMapper(); Map map = mapper.readValue(json, Map.class); for ( Email key : map.values()) { System.out.println(key.getNotificationType()); } – Vitor Darela May 20 '18 at 21:11
  • I can solved this problem with this: mapper.convertValue(map.get(key), Email.class). – Vitor Darela May 20 '18 at 21:38

0 Answers0