当前位置: 首页 > 知识库问答 >
问题:

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

孔波
2023-03-14

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

{ "data": {"id":2," name":"fuchsia rose "," year":2001," color":"#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个答案

孟谭三
2023-03-14

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

用户.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
===============================================


 类似资料: