У меня есть строка json, я хочу получить массив и объекты из 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"
}
]
}
Как видите, он имеет идентификатор, p и имя в качестве объекта и контакты в виде массива
это мои коды в Java:
public interface UserLogin {
@GET("/getLogin3.php")
Call<Contacts>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("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 Contacts withId(String id) {
this.id = id;
return this;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public Contacts withP(String p) {
this.p = p;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Contacts withName(String name) {
this.name = name;
return this;
}
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;
}
}
и это класс активности для чтения:
Call<Contacts>rows=connection.getItems();
rows.enqueue(new Callback<Contacts>() {
@Override
public void onResponse(Call<Contacts> call, Response<Contacts> response) {
Log.v("this", "id" + response.body().getId());
Log.v("this", "name" + response.body().getName());
List<Contact>contacts=response.body().getContacts();
for (int i=0; i<contacts.size();i++){
Log.v("this","result "+ contacts.get(i).getId()+ " - "
+ contacts.get(i).getName());
}
}
@Override
public void onFailure(Call<Contacts> call, Throwable t) {
Log.v("this",t.getMessage());
}
});
это мое соединение:
public class ApiConnection {
private static String BaseUrl = "http://kharident.com";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
Gson gson = new GsonBuilder().setLenient().create();
OkHttpClient client = new OkHttpClient();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
проблема в том, что когда я запускаю код, он не успешен и переходит к методу сбоя, ошибка:
Я проверил JSON в валидаторе JSON, и JSON в порядке
что не так с моим кодом?
Expected name at line 2 column 1 path $.