在Android2中解析JSON数据
我正在尝试在android中使用改型来解析json数据 Json数据:在Android2中解析JSON数据,android,json,Android,Json,我正在尝试在android中使用改型来解析json数据 Json数据: { "data": [ { "id": "9", "type": "vehicle", "attributes": { "name": "Audi", "vehicle_type": "Self Driven", "registrati
{
"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;
}
}
}
public类posturelogin{
@序列化名称(“电子邮件”)
字符串电子邮件;
@序列化名称(“密码”)
字符串密码;
@序列化名称(“名称”)
字符串名称;
@SerializedName(“令牌”)
字符串标记;
@SerializedName(“数据”)
阵列列表数据;
公共ArrayList getData(){
返回数据;
}
公共void setData(ArrayList数据){
这个数据=数据;
}
公共姿势登录(字符串电子邮件、字符串密码、字符串指定){
this.email=电子邮件;
this.password=密码;
这个名称=名称;
}
公共字符串getToken(){
返回令牌;
}
公共void setToken(字符串令牌){
this.token=token;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getDesignation(){
返回指定;
}
公共无效设置名称(字符串名称){
这个名称=名称;
}
公共类数据{
@序列化名称(“id”)
字符串id;
@SerializedName(“属性”)
字符串属性;
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getAttributes(){
返回属性;
}
公共void集合属性(字符串属性){
this.attributes=属性;
}
}
公开课注册{
@序列化名称(“注册”)
字符串regNum;
公共字符串getRegNum(){
返回regNum;
}
公共void setRegNum(字符串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) {
}
});
Call Call=apinterface.set_令牌(令牌);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
ArrayList ArrayList=response.body().getData();
if(arrayList!=null){
对于(int i=0;i
您正在使用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;
数据
类不正确
使用属性作为类的对象,而不是像这样的字符串
public class Data
{
private Attributes attributes;
private String id;
private String type;
public Attributes getAttributes ()
{
return attributes;
}
public void setAttributes (Attributes attributes)
{
this.attributes = attributes;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getType ()
{
return type;
}
public void setType (String type)
{
this.type = type;
}
}
其中属性
类为
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;
}
}
您的数据类错误。请为属性创建数据类,然后您可以获得注册。您可以将JSON
转换为POJO
(模型类)在线…或者你看到JSON中的注册字段了吗?@MD它在attributes objectYes中,我必须为attributes创建一个类。我已经完成了。现在它工作正常。@amar544有一个灰色的勾号,你必须点击它才能接受任何答案。我很高兴它现在工作正常,请上传我的答案供其他人参考
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;
}
}