使用Restofit解析nestes JSON

时间:2017-02-04 21:22:23

标签: java json retrofit

我想问一下解析 Retrofit 中的复杂结构。我的REST中有JSON:

{
  "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
}

我想知道如何将其捕获到我的POJO中。我有3个班级:

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;

    ...
}

我尝试通过返回Call来调用我的GET方法,但它不起作用,只有金额正常工作,我的数组是空的。

我该如何解析?

2 个答案:

答案 0 :(得分:0)

    public void DocLoaded(object obj, GeckoDocumentCompletedEventArgs e)
    {
        Loaded = true;
        if (Gweb.Url != LoginUri) return;

        MutationEventListener mutationListener = new MutationEventListener();
        mutationListener.OnDomMutation += TestObservedEvent;
        nsIDOMEventTarget target = Xpcom.QueryInterface<nsIDOMEventTarget>(/*Lost here*/);
        using (nsAString modified = new nsAString("DOMSubtreeModified"))
            target.AddEventListener(modified, mutationListener, true, false, 0);

        AttemptLogin();
    }

答案 1 :(得分:-1)

尝试使用人员列表而不是数组:

public class Persons {

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