JSON响应体为数组类型,转换为Java对象

huangapple go评论72阅读模式
英文:

JSON Response Body of type array to Java Object

问题

我正在将类型为数组的 JSON 响应转换为 Java Object 类,但在进行反序列化时出现错误:

> com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)

JSON 响应

[
    {
        "name": "Apple iPhone X",
        "price": 700,
        "rating": 4,
        "id": 1
    },
    {
        "name": "Apple Mac Mini",
        "price": 900,
        "rating": 5,
        "id": 2
    },
    {
        "name": "HTC Chacha",
        "price": 200,
        "rating": 3,
        "id": 4
    },
    {
        "name": "Sony Xperia",
        "price": 600,
        "rating": 5,
        "id": 5
    },
    {
        "name": "Samsung Galaxy",
        "price": 400,
        "rating": 2,
        "id": 6
    },
    {
        "name": "LG LED 5600VW",
        "price": 550,
        "rating": 1,
        "id": 7
    },
    {
        "name": "Moto Razor",
        "price": 65000,
        "rating": 4,
        "id": 9
    }
]

Phones.java(Object 类模型)

package apiEngine.model.responses;


public class Phones {

public String name;
public Integer price;
public Integer rating;
public Integer id;

public Phones() {
}

public Phones(String name, Integer price, Integer rating, Integer id) {
super();
this.name = name;
this.price = price;
this.rating = rating;
this.id = id;
}

}

转换方法,将 JSON 响应转换为 Java 对象

private static Phones phoneResponse;
public void displaylist() {
		RequestSpecification request = RestAssured.given();
		request.header("Content-Type", "application/json").header("x-access-token", token);
		response = request.get("/products");
		phoneResponse = response.getBody().as(Phones.class);
		jsonString = response.asString();
		//System.out.println("list of phone is displayed \n" + phoneResponse);
	}
英文:

I am converting JSON response of type array into java Object class, but while doing deserialization i am getting error as

> com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)

JSON Response

[
    {
        "name": "Apple iPhone X",
        "price": 700,
        "rating": 4,
        "id": 1
    },
    {
        "name": "Apple Mac Mini",
        "price": 900,
        "rating": 5,
        "id": 2
    },
    {
        "name": "HTC Chacha",
        "price": 200,
        "rating": 3,
        "id": 4
    },
    {
        "name": "Sony Xperia",
        "price": 600,
        "rating": 5,
        "id": 5
    },
    {
        "name": "Samsung Galaxy",
        "price": 400,
        "rating": 2,
        "id": 6
    },
    {
        "name": "LG LED 5600VW",
        "price": 550,
        "rating": 1,
        "id": 7
    },
    {
        "name": "Moto Razor",
        "price": 65000,
        "rating": 4,
        "id": 9
    }
]

Phones.Java (Object class model)

package apiEngine.model.responses;


public class Phones {

public String name;
public Integer price;
public Integer rating;
public Integer id;

public Phones() {
}

public Phones(String name, Integer price, Integer rating, Integer id) {
super();
this.name = name;
this.price = price;
this.rating = rating;
this.id = id;
}

}

Converting Method where JSON response is being converted into Java object

private static Phones phoneResponse;
public void displaylist() {
		RequestSpecification request = RestAssured.given();
		request.header("Content-Type", "application/json").header("x-access-token", token);
		response = request.get("/products");
		phoneResponse = response.getBody().as(Phones.class);
		jsonString = response.asString();
		//System.out.println("list of phone is displayed \n" + phoneResponse);
	}

答案1

得分: 3

你正在尝试将一个 JSON 数组转换为对象。可以尝试以下方法:

final Phone[] phoneResponses = new Gson().fromJson(jsonString, Phone[].class);

此外,我建议您将 Phones 类的名称重构为 Phone,因为它表示单个手机。

英文:

You are trying to convert a JSON array to an object. Try something like;

final Phones[] phoneResponses = new Gson().fromJson(jsonString, Phones[].class);

Further, I would suggest you refactor the name of your Phones class to Phone because it represents a single phone.

答案2

得分: 1

你正在尝试将JSON array作为object获取 - 你需要将它作为数组获取,不确定对于GSON是否有效,但通常类似于:

phoneResponse = response.getBody().as(Phones[].class);

应该可以工作。

其中phoneResponse必须是Phone类的数组。因此,名称phones可能更合适。

英文:

You are trying to get a JSON array as object - you need to get it as array, not sure if it works for GSON, but usually stuff like:

phoneResponse = response.getBody().as(Phones[].class);

should work.

Where phoneResponse has to be an array of the Phone class. Therefore the name phones"would be probably more suiting.

huangapple
  • 本文由 发表于 2020年8月19日 00:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63472660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定