无法使用getObject [java.lang.ClassCastException将JSON转换为对象:[B无法转换为[C]

时间:2019-05-17 14:50:42

标签: json mapping pojo rest-assured

我在使用getObject将JSON响应转换为对象时遇到问题。 JSON响应如下所示:

{ “数据”: {“ id”:2, “ name”:“紫红色的玫瑰”, “年”:2001年, “颜色”:“#C74375”, “ pantone_value”:“ 17-2031”} }

我正在使用restAssured 2.9.0并尝试过此操作:

public class User {


    String name;
    String color;
    String pantone_value;
    int year;
    int id;
}


 @Test
    public void testUserSerialisation()  {


      User user = given()
              .when()
              .get("https://reqres.in/api/unknown/2")
              .then()
              .extract()
              .response()
              .getBody()
              .jsonPath()
              .getObject("data", User.class);

}

我遇到以下错误:

java.lang.ClassCastException: [B cannot be cast to [C

1 个答案:

答案 0 :(得分:0)

我能够使用用户对象映射JSON响应。

User.java

/**
 * 
 */
package com.apiautomation.framework.titan.models;

/**
 * @author vamsir
 *
 */
public class User {

    public String name;
    public String color;
    public String pantone_value;
    public int year;
    public int id;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }
    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }
    /**
     * @return the pantone_value
     */
    public String getPantone_value() {
        return pantone_value;
    }
    /**
     * @param pantone_value the pantone_value to set
     */
    public void setPantone_value(String pantone_value) {
        this.pantone_value = pantone_value;
    }
    /**
     * @return the year
     */
    public int getYear() {
        return year;
    }
    /**
     * @param year the year to set
     */
    public void setYear(int year) {
        this.year = year;
    }
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

}

ResReqClassMapping.java

/**
 * 
 */
package com.apiautomation.framework.titan.testsuites.samples;

import org.testng.annotations.Test;

import com.apiautomation.framework.titan.models.User;

import io.restassured.RestAssured;

/**
 * @author vamsir
 *
 */
public class ResReqClassMapping {

    @Test
    public void testUserSerialisation()  {


      User user = RestAssured.given()
              .when()
              .get("https://reqres.in/api/unknown/2")
              .then()
              .extract()
              .response()
              .getBody()
              .jsonPath()
              .getObject("data", User.class);


      System.out.print(user.getColor());

}


}


响应

[RemoteTestNG] detected TestNG version 6.14.3
#C74375
PASSED: testUserSerialisation

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================