如何从SeleniumWebDriver中返回json格式数据的API读取数据?
基本上,我使用GET调用启动了一组api,该调用以json格式返回数据,我只想打印特定的字符串,如下面的json片段中提到的hostid如何从SeleniumWebDriver中返回json格式数据的API读取数据?,json,selenium,webdriver,Json,Selenium,Webdriver,基本上,我使用GET调用启动了一组api,该调用以json格式返回数据,我只想打印特定的字符串,如下面的json片段中提到的hostid "hostId" : 286, "parentSectionName" : "adadfr", "rank" : 86096.0, "activationStatus" : "ACTIVE", "overrideLinkProp" : 0, 假设您的json如下所示: String jsonToDecode = "{"
"hostId" : 286,
"parentSectionName" : "adadfr",
"rank" : 86096.0,
"activationStatus" : "ACTIVE",
"overrideLinkProp" : 0,
假设您的json如下所示:
String jsonToDecode = "{" +
" \"hostId\" : 286,\n" +
" \"parentSectionName\" : \"adadfr\",\n" +
" \"rank\" : 86096.0,\n" +
" \"activationStatus\" : \"ACTIVE\",\n" +
" \"overrideLinkProp\" : 0,"+
"}";
您可以使用“com.googlecode.json simple”包对其进行解码,如下所示:
JSONParser parser = new JSONParser();
Object parsedJson = new Object();
try {
parsedJson = parser.parse(jsonToDecode);
} catch (ParseException e) {
//do something when json parsing fails
}
JSONObject jsonObject = (JSONObject) parsedJson;
String name = (String) jsonObject.get("parentSectionName");
System.out.print(name); // will output adadfr
在java中,您可以使用JsonPath:
String jsonToDecode = "{" +
" \"hostId\" : 286,\n" +
" \"parentSectionName\" : \"adadfr\",\n" +
" \"rank\" : 86096.0,\n" +
" \"activationStatus\" : \"ACTIVE\",\n" +
" \"overrideLinkProp\" : 0,"+
"}";
System.out.println(JsonPath.from(jsonToDecode).getString("hostId"));
根据所使用的语言,可以使用各种库来解析
JSON
。挑一个你觉得好看的。它可能是selenium
测试的一部分,但您不会使用selenium
库来处理JSON
对象。