0

Have similar problem to this but i couldnt work this out Getting null values when reading in JSON file in eclipse using java I'm having problem while reading data from this 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);

I just want to get "race_id" and "name" Race class looks like this:

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

Responsy body is perfectly fine but when im trying to convert it to the RaceList object which look like this:

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

And every Race has "race_id" equaled 0 and "name" as null.

RaceList(races=[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), 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), 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), 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)])

1
  • You are missing an object level (class) between RaceList and Race. Commented Sep 19, 2019 at 14:18

2 Answers 2

0

I think your classes should look something like this:

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

public class Race {
    private RaceData race;
}

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

The name of the first race should then be accessed as races.getRaces().get(0).getRace().getName() if races is the name of the variable holding the RaceList object.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks guys, your answers have lead me to, i hope so, best solution:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.