2016-08-25 106 views
1

这是我FragmentJSON解析与GSON返回null

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到你反对上课的时候我加入它给了我一个错误:

Expected String but Found Object

然后,添加\"到serializednames那么它的工作,但现在我只能得到null当我打电话类对象。

回答

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()); 
+0

真棒,工作:)非常感谢。 –

1

为什么在@SerializedName中使用多余的引号?它很可能会破坏你的代码。此外,你尝试序列化'地址'对象作为一个字符串,这将导致“预期的字符串,但发现对象”错误。

解决方法:删除多余的引号,并创建新类:

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

删除\SerializedName

@SerializedName("\"firstName\"") 

应该像

@SerializedName("firstName") 
+0

当我这样做时,它会给出一个错误,说预期的字符串,但找到对象。 –

+0

@AliyasarErdogan看看我上面的答案,有完整的解决方案,这个错误 – pawelo