Java 无法从html响应中提取属性

Java 无法从html响应中提取属性,java,xml,rest-assured,Java,Xml,Rest Assured,我试图从html响应中提取一个属性 <html> <head></head> <body> <script type="text/javascript" src="/sso/js/jquery/1/jquery.min.js?20190218"></script> {serviceUrl: 'https://abcd/12345', serviceTicket: 'ABCD-123-1271821818sdsdbbsdgw3

我试图从html响应中提取一个属性

<html>
<head></head>
<body>
<script type="text/javascript" src="/sso/js/jquery/1/jquery.min.js?20190218"></script>
{serviceUrl: 'https://abcd/12345', serviceTicket: 'ABCD-123-1271821818sdsdbbsdgw3-pas'}
</body>
</html>

我希望上一条语句中的serviceUrl会让我返回,但它会给我空指针异常

要将完整的响应正文作为字符串获取,您需要使用
asString()
方法,而不是
toString()
。下面是一个例子:

Response response =  given()
    .queryParam("logintoken", logintoken)
    .when()
    .get("/sso/login")
    .then().assertThat().statusCode(200).extract().response();

//Extract response body as a string
String html = response.asString();

//Parse extracted html with Jsoup
Document document = Jsoup.parse(html);

//Get <body> element from html
Element body = document.body();

//Extract text from <body> element
String bodyText = body.ownText();

//Parse extracted text using Jackson's ObjectMapper
Map<String, Object> map = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();

//Configure Jackson to work with unquoted fields and single quoted values
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

try {
  map = mapper.readValue(String.valueOf(bodyText), new TypeReference<Map<String, Object>>() {});
} catch (Exception e) {
  e.printStackTrace();
}

System.out.println(map.get("serviceUrl"));
响应=给定()
.queryParam(“logintoken”,logintoken)
.when()
.get(“/sso/login”)
.then().assertThat().statusCode(200.extract().response();
//将响应主体提取为字符串
字符串html=response.asString();
//使用Jsoup解析提取的html
documentdocument=Jsoup.parse(html);
//从html获取元素
元素体=document.body();
//从元素中提取文本
字符串bodyText=body.ownText();
//使用Jackson的ObjectMapper解析提取的文本
Map Map=newhashmap();
ObjectMapper mapper=新的ObjectMapper();
//将Jackson配置为使用无引号字段和单引号值
配置(Feature.ALLOW_SINGLE_QUOTES,true);
配置(Feature.ALLOW\u UNQUOTED\u FIELD\u NAMES,true);
试一试{
map=mapper.readValue(String.valueOf(bodyText),newTypeReference(){});
}捕获(例外e){
e、 printStackTrace();
}
System.out.println(map.get(“serviceUrl”);

在上面的示例中,Jackson的ObjectMapper用于解析
中的文本。您可以在此处阅读更多信息-

谢谢,但我无法获取上述html中属性的属性值“”。{serviceUrl:'',serviceTicket:'ABCD-123-1271821818SDBBSDGW3-pas'}因为它没有将serviceUrl标识为属性。请在这里帮助我。有人可以帮助我使用索引来获取该属性吗?@aak我已经用提取
serviceUrl
值的示例更新了我的答案
Response response =  given()
    .queryParam("logintoken", logintoken)
    .when()
    .get("/sso/login")
    .then().assertThat().statusCode(200).extract().response();

//Extract response body as a string
String html = response.asString();

//Parse extracted html with Jsoup
Document document = Jsoup.parse(html);

//Get <body> element from html
Element body = document.body();

//Extract text from <body> element
String bodyText = body.ownText();

//Parse extracted text using Jackson's ObjectMapper
Map<String, Object> map = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();

//Configure Jackson to work with unquoted fields and single quoted values
mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

try {
  map = mapper.readValue(String.valueOf(bodyText), new TypeReference<Map<String, Object>>() {});
} catch (Exception e) {
  e.printStackTrace();
}

System.out.println(map.get("serviceUrl"));