user6456773 user6456773 - 2 months ago 10
JSON Question

Java: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

SOLVED! Check the answers below

I am working with the GitHub repo, want to access the names of the users' repos. The JSON looks like this:

[
{
"id": 55848012,
"name": "Android",
"full_name": "VyaraGGeorgieva/Android"
...
...
}
]


I have a Repo class

public class Repo {

@SerializedName("name")
private String name;

public String getName() {
return name;
}

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


}

And a class, storing the returned query:

public class GitHubRepos {

private Repo[] repos;

public Repo[] getRepos() {
return repos;
}

public void setRepos(Repo[] repos) {
this.repos = repos;
}


The endPoint is defines as this:

@GET("/users/{user}/repos")
Call<List<GitHubRepos>> getRepoName(@Path("user") String name);


When I try to execute the calling:

call.enqueue(new Callback<List<GitHubRepos>>() {
@Override
public void onResponse(Call<List<GitHubRepos>> call, Response<List<GitHubRepos>> response) {
Repo[] repos = response.body().**getRepos()**;
Log.d("Repos: ", "Number of repos received: " + repos.length);
}


Defined on this way, I can't access the getRepos() method.

I don't define correctly some types, but not sure how to fix the problem.

Answer

you need to pass List instead of Object.

call.enqueue(new Callback<List<GitHubRepos>>() {
        //override method.
    });