Reputation: 410
I have a json
string:
{
"Id": 1,
"Title": "A night with king of France",
"Description": "King's of France awesome audition",
"Day": "Monday",
"Time": "00:00:00",
"FinnishTime": "02:00:00",
"Picture": "http://cp91279.biography.com/1000509261001/1000509261001_1134394072001_Bio-Biography-Louis-XIV-LF-retry.jpg",
"Host1": {
"Id": 1,
"Name": "Louis XIV",
"Description": "The king of France - likes classical music"
}}
and I'm trying to deserialize it using json
Type listType = new TypeToken<Program>() {}.getType();
Program h = new Gson().fromJson(text, listType);
My Program Class looks like this:
private int Id;
private String Title;
private String Description;
private String Day;
private LocalTime Time;
private LocalTime FinnishTime;
private String Picture;
private Host Host1;
Program(int id, String title, String description, Host host, String day, LocalTime startTime, LocalTime finnishTime, String picture)
{
this.Id = id;
this.Title = title;
this.Description = description;
this.Day = day;
this.Time = startTime;
this.FinnishTime = finnishTime;
this.Picture = picture;
this.Host1 = host;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public Host getHost1() {
return Host1;
}
public void setHost1(Host host1) {
Host1 = host1;
}
public String toString() {
return this.Id + ". " + this.Title;
}
public String getDay() {
return Day;
}
public void setDay(String day) {
Day = day;
}
public LocalTime getTime() {
return Time;
}
public void setTime(LocalTime time) {
Time = time;
}
public LocalTime getFinnishTime() {
return FinnishTime;
}
public void setFinnishTime(LocalTime finnishTime) {
FinnishTime = finnishTime;
}
public String getPicture() {
return Picture;
}
public void setPicture(String picture) {
Picture = picture;
EDIT
private int Id;
private String Name;
private String Description;
public Host(int Id, String Name, String Description)
{
this.Id = Id;
this.Name = Name;
this.Description = Description;
}
public int getId() {
return Id;
}
public void setId(int id) {
this.Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
this.Description = description;
}
The error is Expected BEGIN_OBJECT but was STRING at line 1 column 120
I think that the problem is that it confuses Host1
variable with one of the others - but I have no idea why, and how to make it understand in correct way. What does line 1 column 120 mean?
Upvotes: 2
Views: 1785
Reputation: 1259
You have to use a custom Type Adapter!
Try in this way:
JsonDeserializer<LocalTime> localTimeDeserializer = new JsonDeserializer<LocalTime>() {
@Override
public LocalTime deserialize(JsonElement jsonElem, Type type,
JsonDeserializationContext context) throws JsonParseException {
if (jsonElem == null) {
return null;
}
String localTimeStr = jsonElem.getAsString();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.parse(localTimeStr, formatter);
return localTime;
}
};
Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalTime.class, localTimeDeserializer).create();
Type listType = new TypeToken<Program>() {}.getType();
Program program = gson.fromJson(json, listType);
Upvotes: 3
Reputation: 2485
line 1 column 120 is "Time": "00:00:00",
you should use String for Time
and FinnishTime
Upvotes: 1