0

I have a json context like below:

{
  "data": {
    "details": {
      "en-CA": {
        "languageCode": "en-CA",
        "isPrimaryLocale": false
      },
      "en-US": {
        "languageCode": "en-US",
        "isPrimaryLocale": true,
        "languageDisplayName": "English (United States)",
      }
    }
  }
}

To map it with GSON in java:

I created this classes:

public class ApiResponseSingleDto
{
    private ResponseDetail data;
}

public class ResponseDetail
{
    private ResponseDetails details;

    @Getter
    public static class ResponseDetails
    {
        public HashMap<String, LocaleDetail> row = new HashMap<>();
    }
}

public class LocaleDetail
{
    private String languageCode;
    private Boolean isPrimaryLocale;
    private String languageDisplayName;
}

When I try to map json to Java POJO class, HashMap doesn't work. Is there any suggestion?

To map it:

GSON.fromJson("...json", Type type...);

1 Answer 1

1

Just try to replace:

public class ApiResponseSingleDto
{
    private ResponseDetail data;
}

public class ResponseDetail
{
    private Map<String, LocaleDetail> details;
}

public class LocaleDetail
{
    private String languageCode;
    private Boolean isPrimaryLocale;
    private String languageDisplayName;
}

Also json seems to be incorrect: "languageDisplayName": "English (United States)",

should be just "languageDisplayName": "English (United States)"

One more note: I believe you should have public fields or at least getters for them

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.