gpt4 book ai didi

java - 如何从存在其他 webelements 的 test.properties 读取 json 数据

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:32 25 4
gpt4 key购买 nike

我在 test.properties 文件中有一些 WebElement,我需要读取并存储该文件中的所有 json 数据。任何人都可以帮忙从 java 的属性文件中读取所有 json 数据吗?

我尝试了所有读取和存储数据的方法,但没有成功。

这是属性文件中我的 webElements:

mprHomePageLogo=//span[@class='si mpr-logo-h40px' and text()='MyPerfectResume.com']
mprHomePageLogoNewLP=//*[@class='inner-container']/span[@class='logo']
textLabel1=//*[@id='mt_mac_headline']/h1[text()='The Online Resume Builder So Easy to Use']
headingTextLabelOnNewLP=//*[contains(@class,'h1') and contains(text(),'Your Resume, Made')]/../h1/span[text()='Easy']
textLabel2=//*[text()='My Perfect Resume takes the hassle out of resume writing. Easy prompts help you create the perfect job-worthy resume effortlessly!']
desktoplikeImageOnLP=//object[contains(@class,'hero-desktop')]/following-sibling::a
iconsAndArrows=//*[@id='page-header-ctnr']/ul/li[@id="icon1" and @class="icon-ctnr"]/following-sibling::li[@id="arrow1" and @class="arrow-ctnr"]/following-sibling::li[@id="icon2" and @class="icon-ctnr"]/following-sibling::li[@id="arrow2" and @class="arrow-ctnr"]/following-sibling::li[@id="icon3" and @class="icon-ctnr"]
jsonArray= {"card":[{"Name":"1test1","place":"1placetest1"},{"Name":"2test2","place":"placetest2"},{"Name":"3test3","place":"3placetest3"}]}
textLabel3532=//*[text()='My Perfect Resume takes the hassle out of resume writing. Easy prompts help you create the perfect job-worthy resume effortlessly!']
desktoplikeImageOtestnLP=//object[contains(@class,'hero-desktop')]/following-sibling::a
jsonArray2= {"card":[{"Name":"test1","place":"placetest1"},{"Name":"test2","place":"placetest2"},{"Name":"test3","place":"placetest3"}]}

如您所见,此属性文件中有两个 json 数据(jsonArray 和 jsonArray2),我需要读取此 jsonArray 的所有键和值。

最佳答案

首先,您需要从文件中读取值。这可以使用标准 java 包通过以下代码来完成

try {
Properties prop = new Properties();
String propFileName = "test.properties";
inputSecretStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputSecretStream != null) {
prop.load(inputSecretStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}

String jsonArray = prop.getProperty("jsonArray");
String jsonArray2= prop.getProperty("jsonArray2");
} catch (Exception e) {
System.out.println("Exception: " + e);
} finally {
inputSecretStream.close();
}

然后你应该解析你的 JSON。我正在使用 Google 的 Gson 库。它应该看起来像这样

        JsonArray jarray = new JsonParser().parse(jsonArray ).getAsJsonArray();
JsonObject jobject = jarray.getAsJsonObject("Name");

关于java - 如何从存在其他 webelements 的 test.properties 读取 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com