从Rest API读取数据后为空值

时间:2019-09-19 14:08:29

标签: java json gson

有类似的问题,但是我无法解决Getting null values when reading in JSON file in eclipse using java 从此https://runsignup.com/Rest/races?format=json&country=US

读取数据时遇到问题
Gson gson = new GsonBuilder().setPrettyPrinting().create();
        final String racesUri = "https://runsignup.com/Rest/races?format=json&country=US";
        HttpResponse<String> response = HttpClient
                .newBuilder()
                .proxy(ProxySelector.getDefault())
                .build()
                .send(Request.requestGet(racesUri), HttpResponse.BodyHandlers.ofString());
        RaceList races = gson.fromJson(response.body(), RaceList.class);
        System.out.println(response.body());
        System.out.println(races);

我只想获取“ race_id”和“ name” Race类,如下所示:

package stefanowicz.kacper.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Race {
    private long race_id;
    private String name;
}

响应主体非常好,但是当我尝试将其转换为如下所示的RaceList对象时:

package stefanowicz.kacper.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RaceList {
    private List<Race> races;
}

每个种族的“ race_id”等于0,“ name”为null。

RaceList(种族= [Race(种族ID = 0,名称=空),Race(种族ID = 0,名称=空),Race(种族ID = 0,名称=空),Race(种族ID = 0,名称=空) ),种族(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0,name = null),Race(比赛ID = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null) ,种族(race_id = 0,名称= null),种族(race_id = 0,名称= null),种族(race_id = 0,名称= null),种族(race_id = 0,名称= null),种族(race_id = 0) ,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0,name = null),种族(race_id = 0, name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race (race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),R ace(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0, name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race (race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null),Race(race_id = 0,name = null)])

2 个答案:

答案 0 :(得分:0)

我认为您的课程应如下所示:

public class RaceList {
    private List<Race> races;
}

public class Race {
    private RaceData race;
}

public class RaceData {
    private long race_id;
    private String name;
    /* ... */
}

如果races.getRaces().get(0).getRace().getName()是保存races对象的变量的名称,则应该以{{1​​}}的形式访问第一次比赛的名称。

答案 1 :(得分:0)

谢谢大家,您的回答使我找到了最佳解决方案:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class RaceList {
    private List<Map<String, Race>> races;
}