android - 在 android retrofit2 中解析 JSON 数据
问题描述
我正在尝试使用 android 中的改造来解析 json 数据。
json数据:
{
"data": [
{
"id": "9",
"type": "vehicle",
"attributes": {
"name": "Audi",
"vehicle_type": "Self Driven",
"registration": "KA2510101",
"image": "/uploads/vehicle/image/9/audi.jpeg"
}
},
{
"id": "7",
"type": "vehicle",
"attributes": {
"name": "BMW",
"vehicle_type": "Self Driven",
"registration": "KA23MN9806",
"image": "/uploads/vehicle/image/7/low.time1447943958802.jpg"
}
}
}
在这里,我想检索注册数据。
数据模型类
public class PostUserLogin {
@SerializedName("email")
String email;
@SerializedName("password")
String password;
@SerializedName("designation")
String designation;
@SerializedName("token")
String token;
@SerializedName("data")
ArrayList<Data> data;
public ArrayList<Data> getData() {
return data;
}
public void setData(ArrayList<Data> data) {
this.data = data;
}
public PostUserLogin(String email, String password, String designation) {
this.email = email;
this.password = password;
this.designation = designation;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public class Data{
@SerializedName("id")
String id;
@SerializedName("attributes")
String attributes;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAttributes() {
return attributes;
}
public void setAttributes(String attributes) {
this.attributes = attributes;
}
}
public class Registration{
@SerializedName("registration")
String regNum;
public String getRegNum() {
return regNum;
}
public void setRegNum(String regNum) {
this.regNum = regNum;
}
}
}
解析json数据方法:
Call<PostUserLogin> call = apiInterface.set_token(token);
call.enqueue(new Callback<PostUserLogin>() {
@Override
public void onResponse(Call<PostUserLogin> call, Response<PostUserLogin> response) {
ArrayList<PostUserLogin.Data> arrayList = response.body().getData();
if(arrayList != null){
for(int i=0;i<arrayList.size();i++){
Log.d("retrofit","vehicleId = "+arrayList.get(i).getId());
}
}
}
@Override
public void onFailure(Call<PostUserLogin> call, Throwable throwable) {
}
});
当我试图获取 id 时,它工作正常。注册时无法获取任何数据。如何从给定的 json 数据中获取注册数据。
解决方案
@SerializedName("attributes")
String attributes;
你正在使用String
它应该是一个类
public class Attributes {
@SerializedName("name")
@Expose
private String name;
@SerializedName("vehicle_type")
@Expose
private String vehicleType;
@SerializedName("registration")
@Expose
private String registration;
@SerializedName("image")
@Expose
private String image;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVehicleType() {
return vehicleType;
}
public void setVehicleType(String vehicleType) {
this.vehicleType = vehicleType;
}
public String getRegistration() {
return registration;
}
public void setRegistration(String registration) {
this.registration = registration;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
并在您的数据类中使用以下
@SerializedName("attributes")
@Expose
private Attributes attributes;
推荐阅读
- python - 网络服务器只向客户端发送一次数据而不是循环
- c - 使用常量而不是数字 - c
- php - 通过特定的名称服务器获取 dns 记录
- java - 如何使用 xpath 或 java 脚本从 XML 中检索重复节点
- python-3.x - Pandas 数据框中的自动“输入”和“输出”功能?
- c# - 使用 LINQ 排序以查找最旧文件时从列表中过滤文件夹
- google-maps - KML 图层未加载
- python - 从列表中选择以特定字符串格式开头的项目
- flutter - 如何使用上一个和下一个按钮实现 Flutter Cards
- javascript - discord,js 混乱,我想为我的 discord 机器人添加一个状态,但我的代码无法正常工作,我怎样才能为我的机器人创建一个状态?