これは私のFragment
: JSONの解析は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;
}
}
この
はあなたがここに私のJSONを見ることができますPersonClass.javapublic 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;
}
}
です。
[{
"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 FoundObject
その後、私はそれが働いていたが、私はクラスオブジェクトを呼び出すときに、今、私は唯一のnull
を得ることができますserializednamesに\"
を追加します。
素晴らしいです:)ありがとう。 –