Reputation: 13
My JSON response is this :
{
"General": {
"Code": "xyz",
"People": {
"0": {
"Name": "abc"
},
"1": {
"Name": "def..."
},
"AddressData": {
"Street": 123,
"ZIP": 456
}
}
}
}
Objects 1 and 2 are the same Object. They will always have the same attributes. However, they may be more than just two Objects depending on when I pull the JSON. I am not really sure how I can deserialise the data using Gson.
I have tried using:
@SerializedName("People")
@Expose
private People[] People;
Where People is:
private class People {
@SerializedName("Name")
@Expose
private String name;
public People(String name){
this.name = name
}
}
However, I get the error
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1
My General Class is:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class General {
@SerializedName("Code")
@Expose
private String code;
@SerializedName("People")
@Expose
private People[] people;
@SerializedName("AddressData")
@Expose
private AddressData addressData;
private class People {
@SerializedName("Name")
@Expose
private String name;
public People(String name){
this.name = name;
}
}
private class AddressData {
@SerializedName("Street")
@Expose
private String street;
@SerializedName("ZIP")
@Expose
private String zip;
}
}
Upvotes: 0
Views: 550
Reputation: 159175
Write a custom serializer. Here is a full example for the JSON in the question.
class PeopleSerializer implements JsonSerializer<People>, JsonDeserializer<People> {
@Override
public JsonElement serialize(People src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
for (Entry<Integer, PeopleName> entry : src.names.entrySet())
obj.add(String.valueOf(entry.getKey()), context.serialize(entry.getValue()));
obj.add("AddressData", context.serialize(src.addressData));
return obj;
}
@Override
public People deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
People people = new People();
for (Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
if (entry.getKey().equals("AddressData"))
people.addressData = context.deserialize(entry.getValue(), AddressData.class);
else
people.names.put(Integer.valueOf(entry.getKey()), context.deserialize(entry.getValue(), PeopleName.class));
}
return people;
}
}
Test
public class Test {
public static void main(String[] args) throws Exception {
String json = "{\r\n" +
" \"General\": {\r\n" +
" \"Code\": \"xyz\",\r\n" +
" \"People\": {\r\n" +
" \"0\": {\r\n" +
" \"Name\": \"abc\"\r\n" +
" },\r\n" +
" \"1\": {\r\n" +
" \"Name\": \"def...\"\r\n" +
" },\r\n" +
" \"AddressData\": {\r\n" +
" \"Street\": 123,\r\n" +
" \"ZIP\": 456\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
"}";
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
Root root = gson.fromJson(json, Root.class);
System.out.println(root);
gson.toJson(root, System.out);
}
}
class Root {
General general;
@Override
public String toString() {
return "Root[general=" + this.general + "]";
}
}
class General {
String code;
People people;
@Override
public String toString() {
return "General[code=" + this.code + ", people=" + this.people + "]";
}
}
@JsonAdapter(PeopleSerializer.class)
class People {
Map<Integer, PeopleName> names = new TreeMap<>();
AddressData addressData;
@Override
public String toString() {
return "People[names=" + this.names + ", addressData=" + this.addressData + "]";
}
}
class PeopleName {
String name;
@Override
public String toString() {
return "PeopleName[name=" + this.name + "]";
}
}
class AddressData {
Integer street;
@SerializedName("ZIP")
Integer zip;
@Override
public String toString() {
return "AddressData[street=" + this.street + ", zip=" + this.zip + "]";
}
}
Output
Root[general=General[code=xyz, people=People[names={0=PeopleName[name=abc], 1=PeopleName[name=def...]}, addressData=AddressData[street=123, zip=456]]]]
{"General":{"Code":"xyz","People":{"0":{"Name":"abc"},"1":{"Name":"def..."},"AddressData":{"Street":123,"ZIP":456}}}}
Upvotes: 1
Reputation: 13
The Object People is a map. Objects 1 and 2 are the keys. I'm going to sleep now :)
Upvotes: 0