0
我米試圖在企業消息解決方案的API(msg91)內的特定值:獲取平衡的API,其中顯示的平衡:獲取文本body標籤
響應:
[{「平衡」 :1000,「route」:「4」,「type」:「SMS」}]
我想從Balance中獲取文本。我怎樣才能使用網絡驅動程序來獲取它。
我米試圖在企業消息解決方案的API(msg91)內的特定值:獲取平衡的API,其中顯示的平衡:獲取文本body標籤
響應:
[{「平衡」 :1000,「route」:「4」,「type」:「SMS」}]
我想從Balance中獲取文本。我怎樣才能使用網絡驅動程序來獲取它。
簡單的字符串
string[] res = response.Split(",")
string balance = new String(res[0].ToCharArray().Where(c => Char.IsDigit(c)).ToArray());
創建一個平衡類:
@Getter
@Setter
public class Balance {
private String balance;
private String route;
private String type;
}
創建BalancePage類使用webdriver的導航頁面,讀取響應,並將其解析爲JSON:
public class BalancePage extends Page {
private List<Balance> balances;
@FindBy(xpath = "html/body")
WebElement response;
BalanceConfig config = create(BalanceConfig.class);
protected BalancePage(TestWebDriver driver) {
super(driver);
testWebDriver.setBaseURL(config.url() + "?authkey=" + config.authKey());
PageFactory.initElements(new AjaxElementLocatorFactory(testWebDriver, 30), this);
}
private String getBalanceString() {
return response.getText();
}
public void getCurrentBalance() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
balances = objectMapper.readValue(getBalanceString(), objectMapper.getTypeFactory().constructCollectionType(List.class, Balance.class));
}
}
public abstract class Page {
public WebDriver testWebDriver;
protected Page(WebDriver driver) {
this.testWebDriver = driver;
}
}
[如何解析Java中的JSON]可能的重複(http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – JeffC