0

tIs there any way to convert nested json to unnested object I got nested json from web service as

{
  name:"name"
  address:{addressName:"address",
           addressList:[{State:"state1",
                         City:"city1"},
                        {State:"state2",
                         City:"city2"}]
          }
}

my domain is like

public class Person{
  private String name;
  private String addressName;
  private String addressState1;
  private String addressCity1;
  private String addressState2;
  private String addressCity2;
}

Because there're only two address will be provided by web service, so we don't need creat another domain as PersonAddress, is there any annotation I can use functionality as inverse of @JsonUnwrapped, so I cast json into my domain.

Neil
  • 2,714
  • 11
  • 29
  • 45

2 Answers2

1

I'm guessing you could use @JsonCreator and @JsonProperty to construct a new instance of Person, but I'm afraid you still need a separate domain classes for PersonAddress and AddressDetails:

PersonAddress:

public class PersonAddress {
 private String addressName;
 private List<AddressDetails> addressList;
}

AddressDetails:

public class AddressDetails {
 private String state;
 private String city;
}

Person:

public class Person {
  private String name;
  private String addressName;
  private String addressState1;
  private String addressCity1;
  private String addressState2;
  private String addressCity2;

  @JsonCreator
  public Person(@JsonProperty("name") String name, @JsonProperty("address") PersonAddress address) {
    this.addressName = address.addressName;
    this.addressState1 = address.addressList.get(0).getState();
    this.addressCity1 = address.addressList.get(0).getCity();
    this.addressState2 = address.addressList.get(1).getState();
    this.addressCity2 = address.addressList.get(1).getCity();
  }
}

See these code examples: http://www.programcreek.com/java-api-examples/index.php?api=org.codehaus.jackson.annotate.JsonCreator

Alin Pandichi
  • 955
  • 5
  • 15
  • Thank you for your response, but the problem is that I don't want to build other domain like PersonAddress and AddressDetails – Neil Aug 14 '15 at 15:40
0

This is almost the same question as How do I deserialize Json with modifying the structure?, except your json structure is different. Please read the explanation there for more details. If your examples are more complicated than the one you've provided in the question, read that full answer to see how to modify it further.

You can use this TypeAdapter<Person> to do the parsing you want:

import java.io.IOException;

import com.google.gson.*;
import com.google.gson.stream.*;

public class PersonTypeAdapter extends TypeAdapter<Person> {
  @Override
  public void write(JsonWriter out, Person value) throws IOException {
    if (value == null) {
      out.nullValue();
      return;
    }

    out.nullValue();
    // exercise for you, if you even need it
  }

  @Override
  public Person read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
      reader.nextNull();
      return null;
    }

    reader.beginObject();
    validateName(reader, "name");
    String name = reader.nextString();
    validateName(reader, "address");
    reader.beginObject();
    validateName(reader, "addressName");
    String addressName = reader.nextString();
    validateName(reader, "addressList");
    reader.beginArray();
    reader.beginObject();
    validateName(reader, "State");
    String addressState1 = reader.nextString();
    validateName(reader, "City");
    String addressCity1 = reader.nextString();
    reader.endObject();
    reader.beginObject();
    validateName(reader, "State");
    String addressState2 = reader.nextString();
    validateName(reader, "City");
    String addressCity2 = reader.nextString();    
    reader.endObject();
    reader.endArray();
    reader.endObject();
    reader.endObject();
    return new Person(name, addressName, 
        addressState1, addressCity1,
        addressState2, addressCity2);
  }

  private void validateName(JsonReader reader, String string) throws IOException {
    String name = reader.nextName();
    if(!string.equals(name)) {
      throw new JsonSyntaxException("Expected: \"" + string + "\", got \"" + name + "\"");
    }
  }
}

Then, just register this with your GsonBuilder:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Person.class, new PersonTypeAdapter());
Gson g = builder.create();
Person person = g.fromJson(jsonString, Person.class);
Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158