整个json文件在执行服务时作为主体读取,而不是获取第一组json数组对象

问题描述 投票:0回答:1

整个JSON文件在执行服务时作为主体读取,而不是获取第一组JSON Array对象

下面是使用的JSON文件:

[
{
    "name": "HHDD",
    "salary": 67087,
    "age": 20
},
{

    "name": "JFGFF",
    "salary": 67087,
    "age": 20
}
]

下面是执行日志,服务已成功执行,但作为响应传递的数据为null:

enter image description here

Json Reader类:

public class ReadJsonFile<T>  {


    /*public static void main (String[] args)
    {
        ReadJsonFile read  = new ReadJsonFile();
        read.getjsondata("src/test/resource/testdataresource/addplacerequest.json", Addbook.class);
    }*/
    public <T> List<T> getjsondata(String filepath, Class<T> typeofT)
    {

         //filepath= "src/test/resource/testdataresource/addplacerequest.json";
        Gson gson = new Gson(); ;
        BufferedReader bufferReader = null;
try {

            bufferReader = new BufferedReader(new FileReader(filepath));
            Type ListType = TypeToken.getParameterized(ArrayList.class, typeofT).getType();
            //gson.toJson(ReadJsonFile,ListType);
            //return new Gson().fromJson(bufferReader, ListType);

             List<T> arraylist = gson.fromJson(bufferReader, ListType); 
            /*Iterator iter = arraylist.iterator();
              while (iter.hasNext()) {
               System.out.println(iter.next());
              }*/
    List<T> output = arraylist;
    System.out.println(output);
    return output;   


    }catch(FileNotFoundException e) {
    throw new RuntimeException("Json file not found at path : " + filepath);
}finally {
    try { if(bufferReader != null) bufferReader.close();}
    catch (IOException ignore) {}
}
    }



}

Pojo课堂:

public class Addbook{


    public String name;
    public int salary;
    public int age;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getSalary() {
        return salary;
    }




    public void setSalary(int salary) {
        this.salary = salary;
    }




    public int getAge() {
        return age;
    }




    public void setAge(int age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "Addbook [name="+ name + ",salary="+ salary +",age="+ age +"]";
    }


}

Pojo和JSON文件类:

public class TestDataBuild {

    public static List<Addbook> addbook(){
    ReadJsonFile<Addbook> readJson=new ReadJsonFile<Addbook>();

    List<Addbook> addbook =  readJson.getjsondata("src/test/resource/testDataResource/addplacerequest.json",Addbook.class);


     System.out.println("Users List-> "+addbook); 
    //System.out.println(addbook.size());

    //Retrieve data from JsonObject and create Employee bean

    for(int i=0; i< addbook.size(); i++) {
        Addbook book =  addbook.get(i);
        System.out.println(book);
        //addbook.get(i).setName(addbook.get(i).name);
        //addbook.get(i).setAge(addbook.get(i).age);
        //addbook.get(i).setSalary(addbook.get(i).salary);
        book.setAge(addbook.get(i).age);
        book.setName(addbook.get(i).name);
        book.setSalary(addbook.get(i).salary);  

        }

    return addbook;


    }

}

主要功能:

res=given().log().all().spec(RequestSpecification())
        //res=given().log().all().header("Content-Type","application/json")
                    .body(TestDataBuild.addbook());
java rest-assured
1个回答
0
投票

在引用之前访问数组:

 List<Addbook> addbook =  readJson.getjsondata("src/test/resource/testDataResource/addplacerequest.json",Addbook.class)[0];
© www.soinside.com 2019 - 2024. All rights reserved.