nanomader
nanomader

Reputation: 410

Mapping specific JSON to Java object

I'm trying to map specific JSON to my Java Model class. And am having trouble mapping it to the Java object.

I'm using fasterxml (jackson) to map JSON to my Java Model class below - CurrencyModel.java. This JSON have '[' at the beginning which it probably means that it is an array. I cannot map it into my class, CurrencyModel.java

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

class CurrencyModel {
    protected List<Currencies> currencies;
    protected List<CurrenciesRates> currenciesRates;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class Currencies {
        @JsonProperty("table")
        private String table;

        @JsonProperty("no")
        private String no;

        @JsonProperty("effectiveDate")
        private String effectiveDate;

        @JsonProperty("rates")
        private ArrayList<CurrencyRatesModel> rates;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class CurrenciesRates {
        @JsonProperty("currency")
        private String currency;

        @JsonProperty("code")
        private String code;

        @JsonProperty("mid")
        private String mid;
    }
}

And having JSON below in String variable

[
  {
    "table": "A",
    "no": "064/A/NBP/2013",
    "effectiveDate": "2013-04-02",
    "rates": [
      {
        "currency": "bat (Tajlandia)",
        "code": "THB",
        "mid": 0.1108
      },
      {
        "currency": "dolar amerykański",
        "code": "USD",
        "mid": 3.2552
      },
      {
        "currency": "dolar australijski",
        "code": "AUD",
        "mid": 3.4048
      }
    ]
  }
]

I'm trying to run this code using:

ObjectMapper objectMapper = new ObjectMapper();
final String output = "[{\"table\": \"A\", \"no\": \"064/A/NBP/2013\",\"effectiveDate\": \"2013-04-02\",\"rates\": [{\"currency\": \"bat (Tajlandia)\",\"code\": \"THB\",\"mid\": 0.1108},{\"currency\": \"dolar amerykański\",\"code\": \"USD\",\"mid\": 3.2552},{\"currency\": \"dolar australijski\",\"code\": \"AUD\",\"mid\": 3.4048}]}]";
List<CurrencyModel> currencyModelList = Arrays.asList(objectMapper.readValue(output, CurrencyModel.class));
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
System.out.println(currencyModelList.toArray());

which results in error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `` out of START_ARRAY token

Upvotes: 1

Views: 2334

Answers (2)

Shaunak Patel
Shaunak Patel

Reputation: 1671

I see problem with your java class. I have executed your input json and come with mentioned below java class mapping.

@Data
@NoArgsConstructor
@AllArgsConstructor

public class CurrencyModel {

    @JsonProperty("table")
    private String table;

    @JsonProperty("no")
    private String no;

    @JsonProperty("effectiveDate")
    private String effectiveDate;

    @JsonProperty("rates")
    private ArrayList<CurrenciesRates> rates;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class CurrenciesRates {
        @JsonProperty("currency")
        private String currency;

        @JsonProperty("code")
        private String code;

        @JsonProperty("mid")
        private String mid;
    }
}

And your main class,

ObjectMapper objectMapper = new ObjectMapper();
        final String output = "[{\"table\": \"A\", \"no\": \"064/A/NBP/2013\",\"effectiveDate\": \"2013-04-02\",\"rates\": [{\"currency\": \"bat (Tajlandia)\",\"code\": \"THB\",\"mid\": 0.1108},{\"currency\": \"dolar amerykański\",\"code\": \"USD\",\"mid\": 3.2552},{\"currency\": \"dolar australijski\",\"code\": \"AUD\",\"mid\": 3.4048}]}]";

        List<CurrencyModel> myObjects = objectMapper.readValue(output, new TypeReference<List<CurrencyModel>>() {
        });

Upvotes: 2

GolamMazid Sajib
GolamMazid Sajib

Reputation: 9437

Your json contains list of object. So use TypeReference

ObjectMapper objectMapper = new ObjectMapper();
    final String output = "[{\"table\": \"A\", \"no\": \"064/A/NBP/2013\",\"effectiveDate\": \"2013-04-02\",\"rates\": [{\"currency\": \"bat (Tajlandia)\",\"code\": \"THB\",\"mid\": 0.1108},{\"currency\": \"dolar amerykański\",\"code\": \"USD\",\"mid\": 3.2552},{\"currency\": \"dolar australijski\",\"code\": \"AUD\",\"mid\": 3.4048}]}]";
    objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    List<CurrencyModel> currencyModelList = objectMapper.readValue(output, new TypeReference<List<CurrencyModel.Currencies>>(){});
    System.out.println(currencyModelList);

Upvotes: 1

Related Questions