gson 如何从Json元素创建域对象?

dfty9e19  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(218)

外部网络服务向我返回一个Json文件,格式为{“预测”:[{“期间_结束”:“2021-01- 15 T01:00:00.000000Z”,“期间”:“PT 30 M”,“ghi 90”:0,“ghi”:0,“ghi 10”:0},{“期间_结束”:“2021-01- 15 T01:30:00.000000Z”,“期间”:“PT 30 M”,“ghi 90”:0,“ghi”:0,“ghi 10”:0},{“期间_结束”:“2021- 15 T02:00:00.000000
使用RestRespone转换json元素
获取(网址)

resp.json instanceof JsonElement

我知道我的 Package 类是
类预测Ghi {

static constraints = {
}

private ArrayList<IrradianciaGlobalHorizontal> forecast

ArrayList<IrradianciaGlobalHorizontal> getForecast() {
    return forecast
}

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

}
和de持久化域类
类辐照度全局水平{

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
}

}
请帮帮忙;多谢了

7z5jn7bk

7z5jn7bk1#

这是您的API实现的问题;终结点更改了域字段名称和/或域名。这将导致重新导入所述数据时出现问题。
该或前端与端点的API文档不匹配。
字段名/域名应与域/资源匹配,除非您要进行某种程度的混淆,然后接受您需要一个中间层来充当翻译(即EDI)。
您希望仅通过更改请求方法,就可以将输出作为输入由同一个端点读取。
我的建议(最简单的解决方案):更改原始端点以匹配域/资源字段名称

k5ifujac

k5ifujac2#

如果你有机会使用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;
    }
}

预测Ghi类。

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;
    }

}

结果:

  • PT 30 M 0型
  • PT 31 M 0型发动机
  • PT 32 M 0型
    相依性等级:
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属性名称是forecasts。在这种情况下,必须使用@JsonProperty(“forecasts”)来修饰属性。如果您不这样做,则会出现如下错误:com.fasterxml.Jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“预测”

相关问题