Android 通过改造获取嵌套的JSON数据
我正在使用这样的改装:Android 通过改造获取嵌套的JSON数据,android,json,android-fragments,android-activity,retrofit,Android,Json,Android Fragments,Android Activity,Retrofit,我正在使用这样的改装: final RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint(URL) .build(); final ApiEndpointInterface apiService = restAdapter.create(ApiEndpointInterface.class); apiService.getData(userId, new Callback<Use
final RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(URL)
.build();
final ApiEndpointInterface apiService = restAdapter.create(ApiEndpointInterface.class);
apiService.getData(userId, new Callback<UserData>() {
@Override
public void success(UserData userData, Response response) {
// get data
}
@Override
public void failure(RetrofitError retrofitError) {
retrofitError.printStackTrace();
}
});
如何在改装成功回调中访问用户名
这是我的模型。用户数据:
使用gsonconverter反序列化嵌套类
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(URL). setConverter(new GsonConverter(new GsonBuilder()
.registerTypeAdapter(UserData.class, new ExplorerDeserializerJson())
.create()))
.build();
public class ExplorerDeserializerJson implements sonDeserializer<UserData> {
@Override
public UserData deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
UserData userdata= new UserData();
JsonArray jsonArray = je.getAsJsonArray();
Log.d(TAG,"Trying");
Gson gson = new Gson();
for(JsonElement element : jsonArray){
JsonObject jsonObject = element.getAsJsonObject();
User userid= gson.fromJson(jsonObject.get("id"), User.class);
userdata.addgallery(userid);
}
return userdata;
}
为什么在变量下面有@SerializedName?id的名称也与json匹配,所以您不需要@SerializedName注释它应该类似于下面的列表user=userData.getData。然后,您可以对其进行迭代,然后将用户名fecth.getData作为一个列表。不应该吗?检查编辑,抱歉我没有注意到List@AkshayMukadam不过,我想知道,既然JSON在数据中总是只返回一个结果,我应该将其作为列表保存还是将其更改为仅限用户?使用此站点。把你的json贴在那里。选择源类型为JSON,选择gson作为注释样式。它将为您提供现成的解析器,这将有助于解决您的困惑。
public class UserData {
@Expose
private Boolean success;
@Expose
private List<User> data = new ArrayList<User>();
/**
*
* @return
* The success
*/
public Boolean getSuccess() {
return success;
}
/**
*
* @param success
* The success
*/
public void setSuccess(Boolean success) {
this.success = success;
}
/**
*
* @return
* The data
*/
public List<User> getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(List<User> data) {
this.data = data;
}
}
public class User {
@Expose
private String id;
@SerializedName("user_id")
@Expose
private String name;
@SerializedName("name")
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The message
*/
public String getName() {
return name;
}
/**
*
* @param message
* The message
*/
public void setName(String name) {
this.name = name;
}
}
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(URL). setConverter(new GsonConverter(new GsonBuilder()
.registerTypeAdapter(UserData.class, new ExplorerDeserializerJson())
.create()))
.build();
public class ExplorerDeserializerJson implements sonDeserializer<UserData> {
@Override
public UserData deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
UserData userdata= new UserData();
JsonArray jsonArray = je.getAsJsonArray();
Log.d(TAG,"Trying");
Gson gson = new Gson();
for(JsonElement element : jsonArray){
JsonObject jsonObject = element.getAsJsonObject();
User userid= gson.fromJson(jsonObject.get("id"), User.class);
userdata.addgallery(userid);
}
return userdata;
}