Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

json - Java GSON Failed parsing to object array

I have a problem. I am doing a webcall where I print a json on my screen like that looks like this:

{"Agents":[{"Id":"1","Owner":"Andre"},{"Id":"7","Owner":"Andre2"},{"Id":"8","Owner":"Andre"},{"Id":"9","Owner":"Alexander"},{"Id":"10","Owner":"Alexander"},{"Id":"12","Owner":"Andre"}]}

Then I used the following code to get the json from the web and parse it into an array object:

EfyWebAPI webAPI = new EfyWebAPI();
String jsonResponse = webAPI.executeQuery("www.mysite.org/test.php", "SELECT Id, Owner FROM Agents");

Gson gson = new Gson();
Agent[] agents = gson.fromJson(jsonResponse, Agent[].class);

System.out.println(agents[0].getId());

The class looks like this:

public class Agent {

    private int id;
    private String owner;

    public Agent(int id, String owner) {
        this.id = id;
        this.owner = owner;
    }

    public int getId() {
        return this.id;
    }

}

But when I run the code, I get the following error:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:822) at com.google.gson.Gson.fromJson(Gson.java:775) at com.google.gson.Gson.fromJson(Gson.java:724) at com.google.gson.Gson.fromJson(Gson.java:696) at com.company.Main.main(Main.java:18) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70) at com.google.gson.Gson.fromJson(Gson.java:810) ... 4 more

Why is this happening and how can I fix this?

question from:https://stackoverflow.com/questions/65909568/java-gson-failed-parsing-to-object-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error is saying

Expected BEGIN_ARRAY...

because of the second argument (Agent[].class), it is expecting a valid json array string of Agent-s. Your json string is an object with a key Agents and the value is an array of Agents.

A possible solution could be for example creating a class named Agents which represents the json object.

public class Agents {
    private Agent[] Agents; // note the capital 'A' matches the key name in the json string

    public Agent[] getAgents() {
        return Agents;
    }

    public void setAgents(Agent[] agents) {
        this.Agents = agents;
    }
}

And adjust the Agent class as follows:

public class Agent {
    @SerializedName("Id") // note the annotated field is needed since in the json string id is Id
    private int id;

    @SerializedName("Owner") // same as id field, annotation needed, or rename owner to Owner
    private String owner;

    public Agent(int id, String owner) {
        this.id = id;
        this.owner = owner;
    }

    public int getId() {
        return this.id;
    }

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

    public void setOwner(String owner) {
        this.owner = owner;
    }

    @Override
    public String toString() {
        return "Agent{" +
                "id=" + id +
                ", owner='" + owner + ''' +
                '}';
    }
}

And here's a working demo:

public class GsonDemo {
    public static void main(String[] args) {
        String json = "{"Agents":[{"Id":"1","Owner":"Andre"},{"Id":"7","Owner":"Andre2"},{"Id":"8","Owner":"Andre"},{"Id":"9","Owner":"Alexander"},{"Id":"10","Owner":"Alexander"},{"Id":"12","Owner":"Andre"}]}";
        Gson gson = new Gson();
        Agents a = gson.fromJson(json, Agents.class);
        System.out.println(a.getAgents()[1]);
        System.out.println(a.getAgents().length);
        // Output:
        // Agent{id=7, owner='Andre2'}
        // 6
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...