猿问

将特定的 JSON 映射到 Java 对象

我正在尝试将特定的 JSON 映射到我的 Java 模型类。并且无法将其映射到 Java 对象。


我正在使用fastxml (jackson) 将 JSON 映射到我下面的 Java 模型类 - CurrencyModel.java。这个 JSON 以 ' [ ' 开头,这可能意味着它是一个数组。我无法将它映射到我的类 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;

    }

}

并且在 String 变量下面有 JSON


[

  {

    "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

      }

    ]

  }

]

我正在尝试使用以下方法运行此代码:

茅侃侃
浏览 121回答 2
2回答

慕标5832272

我看到你的 java 类有问题。我已经执行了您的输入 json 并附带了下面提到的 java 类映射。@Data@NoArgsConstructor@AllArgsConstructorpublic class CurrencyModel {&nbsp; &nbsp; @JsonProperty("table")&nbsp; &nbsp; private String table;&nbsp; &nbsp; @JsonProperty("no")&nbsp; &nbsp; private String no;&nbsp; &nbsp; @JsonProperty("effectiveDate")&nbsp; &nbsp; private String effectiveDate;&nbsp; &nbsp; @JsonProperty("rates")&nbsp; &nbsp; private ArrayList<CurrenciesRates> rates;&nbsp; &nbsp; @Data&nbsp; &nbsp; @NoArgsConstructor&nbsp; &nbsp; @AllArgsConstructor&nbsp; &nbsp; static class CurrenciesRates {&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty("currency")&nbsp; &nbsp; &nbsp; &nbsp; private String currency;&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty("code")&nbsp; &nbsp; &nbsp; &nbsp; private String code;&nbsp; &nbsp; &nbsp; &nbsp; @JsonProperty("mid")&nbsp; &nbsp; &nbsp; &nbsp; private String mid;&nbsp; &nbsp; }}还有你的主课,ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; 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}]}]";&nbsp; &nbsp; &nbsp; &nbsp; List<CurrencyModel> myObjects = objectMapper.readValue(output, new TypeReference<List<CurrencyModel>>() {&nbsp; &nbsp; &nbsp; &nbsp; });

元芳怎么了

您的 json 包含对象列表。所以用TypeReferenceObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; 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}]}]";&nbsp; &nbsp; objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);&nbsp; &nbsp; List<CurrencyModel> currencyModelList = objectMapper.readValue(output, new TypeReference<List<CurrencyModel.Currencies>>(){});&nbsp; &nbsp; System.out.println(currencyModelList);
随时随地看视频慕课网APP

相关分类

Java
我要回答