user2386226
user2386226

Reputation:

how to retrieve nested json object with retrofit?

I am using retrofit to retrieve json object. However, I was wondering if there is an easy way to retrieve nested objects.

Here's my JSON string:

{
  "name": "125 8th avenue",
  "address": "125 8th avenue, San fran ,CA 09012",
  "location": {
    "lon": -72.98013329999998,
    "lat": 45.7552112
  },
  "email": "[email protected]",
  "primaryContact": {
    "firstName": "john",
    "lastName": "doe",
    "jobTitle": "General Manager, 8th Ave",
    "email": "[email protected]",
    "photo": "//images.ctfassets.net/qykmdxxsgb04/3EaIeJ29djgo6Exve4Q7xb.jpeg"
  }

I am retrieving the name and email as :

@Expose
    @SerializedName("name")
    private String name;

    @Expose
    @SerializedName("email")
    private String email;

public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MyInfo)) return false;

        MyInfo that = (MyInfo) o;

        if (!name.equals(that.name)) return false;


    }
@Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + email.hashCode();
return result;
}

As you see from JSON, its pretty straightforward to retrieve name and email but not sure how I can easily retrieve primaryContact details(say firstname and lastname) within the same file ? Any ideas?

Thanks in advance

Upvotes: 0

Views: 1532

Answers (2)

Angel Koh
Angel Koh

Reputation: 13505

Two ways:

  1. make contact and location an inner class (same file), but the fields will still not be easily accessed from outside.

  2. you can instead create a method to access the attributes in contacts from Address.

I use http://www.jsonschema2pojo.org/ to auto generate the files below

public class Address {

    // create a method here to get first/last name
    public String getFirstName(){ 
        return primaryContact==null? "" : 
               primaryContact.getFirstName(); 
    }
    // do the same for which ever inner attributes you like to access.

@SerializedName("name")
@Expose
private String name;
@SerializedName("address")
@Expose
private String address;
@SerializedName("location")
@Expose
private Location location;
@SerializedName("email")
@Expose
private String email;
@SerializedName("primaryContact")
@Expose
private PrimaryContact primaryContact;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public PrimaryContact getPrimaryContact() {
return primaryContact;
}

public void setPrimaryContact(PrimaryContact primaryContact) {
this.primaryContact = primaryContact;
}

}
-----------------------------------com.example.Location.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Location {

@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("lat")
@Expose
private Double lat;

public Double getLon() {
return lon;
}

public void setLon(Double lon) {
this.lon = lon;
}

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

}
-----------------------------------com.example.PrimaryContact.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class PrimaryContact {

@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("jobTitle")
@Expose
private String jobTitle;
@SerializedName("email")
@Expose
private String email;
@SerializedName("photo")
@Expose
private String photo;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getJobTitle() {
return jobTitle;
}

public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoto() {
return photo;
}

public void setPhoto(String photo) {
this.photo = photo;
}

}

Upvotes: 0

Archie G. Quiñones
Archie G. Quiñones

Reputation: 13668

You also have to create your primaryContact Object and serialize it the same way with @Expose and @SerializedName("whatever"). Then Add the primaryContact to that class that you have the and serialize it with the correct name.

Its basically same as how the json is nested. SO instead of nested JSON, you do nested Objects.

Upvotes: 3

Related Questions