无法反序列化JSON数组。

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

Cannot Deserialize a JSON Array

问题

我正在尝试将这个JSON响应存储到一个对象中

[
{
"id": 52,
"disponibilita": 1,
"prezzo": 9
}
]

这是一个数组,我正在为它创建一个POJO类

private String id;
private String prezzo;
private String disponibilita;
private String titolo;

在主方法中,我尝试创建一个POJO类的对象并对其进行反序列化

poj = given().relaxedHTTPSValidation().header("Content-Type", "application/json")
.header("customerId", "xxx").header("S2S-Authorization", res4).expect()
.defaultParser(Parser.JSON).when()
.get("xxx").then()
.log().all().extract().response().as(pojode.class);

List myList = poj.getAction();

System.out.println(myList);

收到以下错误

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Practise.pojode out of START_ARRAY token

英文:

Iam trying to store this JSON Response in an Object

[
    {
        "id": 52,
        "disponibilita": 1,
        "prezzo": 9
    }
]

This is an Array and i am Creating a POJO Clss for it

private  String id ; 
private String prezzo ; 
private String  disponibilita ;
private String titolo;

In the main Method i am trying to create a object for the pojo class and deserialize it

poj = given().relaxedHTTPSValidation().header("Content-Type", "application/json")
				.header("customerId", "xxx").header("S2S-Authorization", res4).expect()
				.defaultParser(Parser.JSON).when()
				.get("xxx").then()
				.log().all().extract().response().as(pojode.class);

		List<String> myList  = poj.getAction();
		
		System.out.println(myList);

Getting the Below Error

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Practise.pojode out of START_ARRAY token

答案1

得分: 0

需要将 JSON 数组 [] 映射到 Java 数组或集合。

pojode[] poj = given()...as(pojode[].class);
System.out.println(Arrays.toString(poj));
英文:

You need to map json array [] to Java array or Collection.

pojode[] poj = given()...as(pojode[].class);
System.out.println(Arrays.toString(poj));

huangapple
  • 本文由 发表于 2023年7月13日 16:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677522.html
匿名

发表评论

匿名网友

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

确定