2017-02-04 4 views
-3

Ich möchte über die Analyse komplexer Struktur in Retrofit fragen. Ich habe JSON von meinem REST:Parsen nestes JSON mit Restofit

{ 
    "persons" : [ { 
    "name" : "Ivan", 
    "age" : 24, 
    "address" : { 
     "country" : "Russia", 
     "city" : "Moscow" 
    }, 
    "birthDate" : "1992-02-07T00:00:00" 
    }, { 
    "name" : "Katarina", 
    "age" : 27, 
    "address" : { 
     "country" : "Russia", 
     "city" : "Petersburg" 
    }, 
    "birthDate" : "1989-08-15T00:00:00" 
    } 
], 
"amount": 2 
} 

Und ich frage mich, wie kann ich erfassen diese zu meinen POJOs. Ich habe 3 Klassen bekommt:

public class Persons { 

    private Person[] persons; 
    private int amount; 
    ... getters, setters... 
} 

public class Person { 

    private String name; 
    private int age; 
    private Address address; 
    private Date birthDate; 
    ... 
} 

public class Address { 

    private String country; 
    private String city; 

    ... 
} 

Ich versuchte, meine GET-Methode Call by Call Rückkehr, aber es funktioniert nicht, nur Menge funktioniert gut, meine Reihe von Personen null ist.

Wie kann ich das analysieren?

+0

Was ist ** Restofit **? Davon habe ich noch nie gehört. Geben Sie irgendwelche Links – Andremoniy

+0

@Andremoniy Mein schlecht, tut mir leid. –

+0

Welche Art von Konverter verwenden Sie? – Andremoniy

Antwort

-1

Versuchen Liste der Personen statt Array:

public class Persons { 

    private List<Person> persons; 
    private int amount; 
    ... getters, setters... 
} 
+0

Ich habe versucht, auch Nullen und weiß nicht warum. –

+0

Haben Sie Getter-Setter für die Personenklasse? –

+0

Ich habe Getter-Setter für jedes Objekt in meiner Klasse, also, in diesem Fall habe ich g/s für Liste von Personen und Betrag, in Person, die ich für jedes Feld habe, usw. –

0
class Persons { 

    private Person[] persons; 

    public Person[] getPersons() { 
     return persons; 
    } 

    public void setPersons(Person[] persons) { 
     this.persons = persons; 
    } 

    public int getAmount() { 
     return amount; 
    } 

    public void setAmount(int amount) { 
     this.amount = amount; 
    } 

    private int amount; 
} 

class Person { 

    private String name; 

    public String getName() { 
     return name; 
    } 

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

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public Address getAddress() { 
     return address; 
    } 

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

    public Date getBirthDate() { 
     return birthDate; 
    } 

    public void setBirthDate(String birthDate) { 
     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     try { 
      this.birthDate = format.parse(birthDate.replace("T"," ")); 
      System.out.println("brith"); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 

    private int age; 
    private Address address; 
    private Date birthDate; 
} 

class Address { 

    private String country; 

    public String getCountry() { 
     return country; 
    } 

    public void setCountry(String country) { 
     this.country = country; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    private String city; 
}