这是我的Fragment
:
public class PersonnelListFragment extends Fragment {
public TextView test;
private View root;
public PersonnelListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_personnel_list, container, false);
Ion.with(getActivity()).load("https://gist.githubusercontent.com/anonymous/e08f8a1b06319d109e0be8561d3948ce/raw/e459314b4bf8d45e82fec783b5435e6e82e6a1bc/p.json")
.asJsonArray()
.setCallback(new FutureCallback<JsonArray>() {
@Override
public void onCompleted(Exception e, JsonArray result) {
Gson gson = new Gson();
Type listType = new TypeToken<List<PersonClass>>() {}.getType();
List<PersonClass> gsonResponse = gson.fromJson(result, listType);
test = (TextView) root.findViewById(R.id.test_text_view);
test.setText(gsonResponse.toString()+"\n"+gsonResponse.get(0).firstName+"\n"+gsonResponse.get(0).getFirstName()+"\n"+result);
}
});
return root;
}
}
这是PersonClass.java
public class PersonClass {
@SerializedName("\"firstName\"")
public String firstName;
@SerializedName("\"lastName\"")
public String lastName;
@SerializedName("\"age\"")
public int age;
@SerializedName("\"photo\"")
public String photo;
@SerializedName("\"address\"")
public String address;
@SerializedName("\"streetAddress\"")
public String streetAddress;
@SerializedName("\"city\"")
public String city;
@SerializedName("\"phoneNumber\"")
public String phoneNumber;
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setAge(int age){
this.age = age;
}
public Integer getAge(){
return age;
}
public void setPhoto(String photo){
this.photo = photo;
}
public String getPhoto(){
return photo;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setStreetAddress(String streetAddress){
this.streetAddress = streetAddress;
}
public String getStreetAddress(){
return streetAddress;
}
public void setCity(String city){
this.city = city;
}
public String getCity(){
return city;
}
public void setPhoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
}
public String getPhoneNumber(){
return phoneNumber;
}
}
你可以在这里看到我的JSON。
[{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"photo": "http://images.mastermp3.net/artwork/nw/nw2560459789391eb5ad57c83786fa8156.jpg",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York"
},
"phoneNumber": "212 555-1234"
}, {
"firstName": "Jane",
"lastName": "Doe",
"age": 27,
"photo": "http://socialventurepartners.s3-us-west-2.amazonaws.com/www.socialventurepartners.org/sites/43/2013/08/Jane-Ragle.jpg",
"address": {
"streetAddress": "17 3rd Street",
"city": "Washington"
},
"phoneNumber": "646 555-4567"
}]
我调查过像我这样的很多问题,但找不到解决方案。人们大多说在我添加它时向你的对象类添加@SerializedName
给了我一个错误:
预计
String
但找到Object
然后我将\"
添加到序列化名称然后它可以工作但现在我只能在调用类对象时获得null
。
答案 0 :(得分:1)
为什么在@SerializedName中使用额外的引号?它很可能会破坏你的代码。此外,您尝试将'address'对象序列化为String,这将导致您出现“Expected String but Found Object”错误。
解决方案:删除多余的引号并创建新类:
<强> Address.java 强>
public class Address {
public String streetAddress;
public String city;
}
<强> PersonClass.java 强>
@SerializedName("address")
public Address address;
然后你可以访问这样的地址字段:
person.address.city;
person.address.streetAddress;
答案 1 :(得分:1)
从\
SerializedName
@SerializedName("\"firstName\"")
它应该像
@SerializedName("firstName")
答案 2 :(得分:1)
地址是JSONObject
,但您尝试将其反序列化为String
。
这样的事情应该有效:
地址 POJO:
public class Address {
private String streetAddress;
private String city;
}
人 POJO:
public class Person {
private String firstName;
private String lastName;
private int age;
private String photo;
private Address address;
private String phoneNumber;
}
反序列化:
List<Person> persons = new Gson()
.fromJson(result, new TypeToken<List<Person>>(){}.getType());