【问题标题】:How to create a domain object from a Json element?如何从 Json 元素创建域对象?
【发布时间】:2021-04-20 01:56:06
【问题描述】:

外部 Web 服务返回一个 Json 文件,格式为 {"预测":[{"period_end":"2021-01-15T01:00:00.0000000Z","period":"PT30M","ghi90":0,"ghi":0,"ghi10":0} ,{"period_end":"2021-01-15T01:30:00.0000000Z","period":"PT30M","ghi90":0,"ghi":0,"ghi10":0},{"period_end" :"2021-01-15T02:00:00.0000000Z","周期":"PT30M","ghi90":0,"ghi":0,"ghi10":0}]}

使用 RestRespone 转换一个 json 元素

RestResponse resp = rest.get(url)

  resp.json instanceof JsonElement
   

如果知道我的包装类是

,如何从 Json 元素变量创建域对象

类 ForecastGhi {

static constraints = {
}

private ArrayList<IrradianciaGlobalHorizontal> forecast


ArrayList<IrradianciaGlobalHorizontal> getForecast() {
    return forecast
}

void setForecast(ArrayList<IrradianciaGlobalHorizontal> forecast) {
    this.forecast = forecast
}

}

并且 de persist 域类是

类 IrradianciaGlobalHorizo​​ntal {

static constraints = {
}
@JsonProperty("all")  

private def period_end
private def period
private def ghi90
private def ghi
private def ghi10

def getGhi() {
     this.ghi
}

void setGhi(int ghi) {
    this.ghi = ghi
}

def getGhi90() {
    this.ghi90
}

void setGhi90(int ghi90) {
    this.ghi90 = ghi90
}

def getGhi10() {
    this.ghi10
}

void setGhi10(int ghi10) {
    this.ghi10 = ghi10
}

def getPeriod_end() {
     this.period_end
}

void setPeriod_end(Date period_end) {
    this.period_end = period_end
}

def getPeriod() {
    this.period
}

void setPeriod(String period) {
    this.period = period
}

}

请帮忙;非常感谢

【问题讨论】:

标签: java grails groovy gson jsonelement


【解决方案1】:

如果你有机会使用 Jackson 库,你可以这样做:

ForecastGhi request = objectMapper.readValue(jsonAsText, ForecastGhi.class);

创建一个objectMapper并配置为在未知属性的情况下失败(以防万一)

private String getJsonAsTextFromRest() {
        String message = " {\"forecasts\":[{\"period_end\":\"2021-01-15T01:00:00.0000000Z\",\"period\":\"PT30M\",\"ghi90\":0,\"ghi\":0,\"ghi10\":0},{\"period_end\":\"2021-01-15T01:30:00.0000000Z\",\"period\":\"PT31M\",\"ghi90\":0,\"ghi\":0,\"ghi10\":0},{\"period_end\":\"2021-01-15T02:00:00.0000000Z\",\"period\":\"PT32M\",\"ghi90\":0,\"ghi\":0,\"ghi10\":0}]}";
        return message;
    }

 @Override
    public void run(String... arg0) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        String jsonAsText = getJsonAsTextFromRest();
        ForecastGhi request = objectMapper.readValue(jsonAsText, ForecastGhi.class);
        request.getForecast().stream().forEach(it -> System.out.println(it.getPeriod() + " " + it.getGhi()));
        
    }

public class IrradianciaGlobalHorizontal {
    private Date period_end;
    private String period;
    private int ghi90;
    private int ghi;
    private int ghi10;

    public int getGhi() {
        return this.ghi;
    }

    public void setGhi(int ghi) {
        this.ghi = ghi;
    }

    public  int getGhi90() {
        return this.ghi90;
    }

    public void setGhi90(int ghi90) {
        this.ghi90 = ghi90;
    }

    public int getGhi10() {
        return this.ghi10;
    }

    void setGhi10(int ghi10) {
        this.ghi10 = ghi10;
    }

    public Date getPeriod_end() {
        return this.period_end;
    }

    public void setPeriod_end(Date period_end) {
        this.period_end = period_end;
    }

    public String getPeriod() {
        return this.period;
    }

    public void setPeriod(String period) {
        this.period = period;
    }
}

ForecastGhi 类。

import com.fasterxml.jackson.annotation.JsonProperty;

public class ForecastGhi {

    private ArrayList<IrradianciaGlobalHorizontal> forecast;

    @JsonProperty("forecasts")//It must be the same as the json property
    public ArrayList<IrradianciaGlobalHorizontal> getForecast() {
        return forecast;
    }

    @JsonProperty("forecasts")
    public void setForecast(ArrayList<IrradianciaGlobalHorizontal> forecast) {
        this.forecast = forecast;
    }

}

结果:

  • PT30M 0
  • PT31M 0
  • PT32M 0

依赖 Gradle:

  compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.12.1'

或者

依赖关系 Maven:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.12.1</version>
</dependency>

注意:在您的 json 示例中,您使用 forecasts,但您的 java 属性名称是 forecast。在这种情况下,有必要使用 @JsonProperty("forecasts") 来装饰属性。如果你不这样做,你会得到一个像这样的错误 com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "forecasts"

【讨论】:

    【解决方案2】:

    这是您的 API 实现的问题;端点更改了域字段名称和/或域名。这将导致将所述数据重新带回的问题。

    该或前端与端点的 API 文档不匹配。

    字段名称/域名应与域/资源匹配,除非您要进行一定程度的混淆,然后接受您需要一个中间层来充当翻译器(即 EDI)。

    您希望输出能够被同一端点读取为输入,只需更改请求方法即可。

    我的建议(最简单的解决方案):更改原始端点以匹配域/资源字段名称

    【讨论】:

    • 你是对的!!优秀 !!谢谢你,它有效!
    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多