gpt4 book ai didi

android - 如何读取 JSON 数组?

转载 作者:行者123 更新时间:2023-11-29 23:38:53 24 4
gpt4 key购买 nike

我想用 Retrofit 2 读取一个 JSON 数组,这是我的 JSON 示例:

{
"id":"58",
"p":"4297f44b13955235245b2497399d7a",
"name":"0634063306cc06340634063306cc",
"contacts" : [
{
"id":"1",
"name":"test1"
},
{
"id":"2",
"name":"test2"
},
{
"id":"3",
"name":"test3"
},
{
"id":"4",
"name":"test4"
},
{
"id":"5",
"name":"test5"
}
]
}

它有一些对象和一个名为contacts 的数组。

这是我读取对象的代码:

public interface UserLogin {
@GET("/getLogin3.php")
Call<UserItems>getUser(@Query("e") String user, @Query("p")String pass);

}
public interface UserLogin {
@GET("/getLogin3.php")
Call<UserItems>getUser(@Query("e") String user, @Query("p")String pass);

@GET("/getLogin3.php")
Call<List<SimpleItem>>getItems();
}

这些是我的联系人和联系人类,如下所示

    public class Contact {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Contact withId(String id) {
this.id = id;
return this;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Contact withName(String name) {
this.name = name;
return this;
}

}

public class Contacts {

@SerializedName("contacts")
@Expose
private List<Contact> contacts = null;

public List<Contact> getContacts() {
return contacts;
}

public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}

public Contacts withContacts(List<Contact> contacts) {
this.contacts = contacts;
return this;
}

}

Activity :

    Call<List<Contacts>>rows=connection.getItems();
rows.enqueue(new Callback<List<Contacts>>() {
@Override
public void onResponse(Call<List<Contacts>> call, Response<List<Contacts>> response) {
List<Contact>contacts=response.body().getContacts();

}

@Override
public void onFailure(Call<List<Contacts>> call, Throwable t) {

}
});

我已经像这样调用了我的 Activity ,但它没有找到 getContacts() 方法。

最佳答案

这样做:

联系方式

public class Contact {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}

简单项目

public class SimpleItem {

@SerializedName("id")
@Expose
private String id;
@SerializedName("p")
@Expose
private String p;
@SerializedName("name")
@Expose
private String name;
@SerializedName("contacts")
@Expose
private List<Contact> contacts = null;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getP() {
return p;
}

public void setP(String p) {
this.p = p;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Contact> getContacts() {
return contacts;
}

public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}

}

界面

@GET("/getLogin3.php")
Call<SimpleItem>getItems();

改造调用

     Call<SimpleItem>rows=connection.getItems();
rows.enqueue(new Callback<SimpleItem>() {
@Override
public void onResponse(Call<SimpleItem> call, Response<SimpleItem> response) {
SimpleItem simpleItem = response.body();
List<Contact>contacts=simpleItem.getContacts();

}

@Override
public void onFailure(Call<SimpleItem> call, Throwable t) {

}
});

关于android - 如何读取 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com