【问题标题】:Restassured + Failed to parse the JSON document放心 + 解析 JSON 文档失败
【发布时间】:2020-05-25 18:08:51
【问题描述】:

在通过放心库执行 POST 时,我收到以下错误:-

放心 + 无法解析 JSON 文档 + groovy.json.JsonException:Lexing 在第 1 行,第 1 列失败,在读取“h”时,无法识别可能的有效 JSON 值或标点符号。

“有效负载”类中提到了有效负载。请帮助我解决这个 JSON 解析问题。 我能够成功 POST,但是在通过 Jsonpath 类检索数据时,它会抛出主题行中提到的错误。

package files;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;

import static io.restassured.RestAssured.*;

public class DynamicJson {

    @Test
    public void addBook(){

        String response1 = RestAssured.baseURI="http://216.10.245.166";
        given().log().all().header("Content-Type","application/json")
        .body(Payload.Addbook())
        .when().post("Library/Addbook.php")
        .then()
        .log().all().assertThat().statusCode(200)
        .extract().response().asString();


        JsonPath js1 = new JsonPath(response1);
        String id = js1.get("ID");
        System.out.println(id);





    }

}

package files;

public class Payload {


    public static String Addbook(){

        String payload = "{\r\n" + 
                "   \"name\":\"Learn Appium Automation with Java\",\r\n" + 
                "   \"isbn\":\"bcd\",\r\n" + 
                "   \"aisle\":\"29k27\",\r\n" + 
                "   \"author\":\"John foe\"\r\n" + 
                "}";
        return payload;

    }
}

【问题讨论】:

    标签: json rest-assured jsonparser


    【解决方案1】:

    这是一个很小的错误

    您正在解析字符串 response1 上的 JSON,这是不正确的

    改成

    String response1 = RestAssured.baseURI="http://216.10.245.166";
    

    RestAssured.baseURI = "http://216.10.245.166";
    String response1 = given().header().....
    

    你的其余代码都很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2015-10-05
      • 2021-05-30
      • 1970-01-01
      • 2017-07-27
      • 2015-10-19
      • 2015-01-19
      相关资源
      最近更新 更多