如果我想提取JSON响应值并避免在开始时使用静态变量,如何在重新介绍中使用hashmap?
现在,我使用JSON路径函数来获取JSON响应值,例如PlaceID,ProfileId,然后将其声明为静态,因为我需要在clickprofile()和clickplace()函数中使用这些变量。展望未来,我将拥有很多值,例如PlaceID,ProfileId,ContentId等,我想避免在开始时声明为静态字符串的值。取而代之的是,我想在此处使用地图概念,并想从Framework实用程序中使用它。 
我是哈希图的新手。有人可以在这里帮助我在这里实施地图概念吗?
public static String placeid;
public static String profileid;
public void extractplaceId()
{
placeid = getJsonPath("results.place_id");
System.out.println(placeid);
}
public void extractpleId()
{
profileid = getJsonPath("results.profile_id");
System.out.println(profileid);
}
public Response clickProfile() throws IOException
{
config.setProperty("APIendPoints.properties");
PROFILE_URL = config.getConfig().getProperty("CLICK_PROFILE") + profileid + ".json";
response = requestSpec.when().get(PROFILE_URL);
return response;
}
public Response clickPlace() throws IOException
{
config.setProperty("APIendPoints.properties");
PLACE_URL = config.getConfig().getProperty("CLICK_PLACE") + placeid + ".json";
response = requestSpec.when().get(PLACE_URL);
return response;
}
框架实用程序:
public String getJsonPath(String key)
{
String resp = response.asString();
JsonPath js = new JsonPath(resp);
return js.get(key).toString();
}
Right now I am using the JSON path function to get the JSON response values like placeid, profileid and then am declaring that as static, because I need to use those variables in clickProfile() and clickPlace() functions. Going forward I will have a lot of values like placeid, profileid, contentid etc and I want to avoid those declaring as static String at the beginning. Instead I want to use the Maps concept here going forward and want to use it from framework Utility.
I am new to HashMaps. Can someone help me here in implementing the Maps concept here?
public static String placeid;
public static String profileid;
public void extractplaceId()
{
placeid = getJsonPath("results.place_id");
System.out.println(placeid);
}
public void extractpleId()
{
profileid = getJsonPath("results.profile_id");
System.out.println(profileid);
}
public Response clickProfile() throws IOException
{
config.setProperty("APIendPoints.properties");
PROFILE_URL = config.getConfig().getProperty("CLICK_PROFILE") + profileid + ".json";
response = requestSpec.when().get(PROFILE_URL);
return response;
}
public Response clickPlace() throws IOException
{
config.setProperty("APIendPoints.properties");
PLACE_URL = config.getConfig().getProperty("CLICK_PLACE") + placeid + ".json";
response = requestSpec.when().get(PLACE_URL);
return response;
}
Framework Utility:
public String getJsonPath(String key)
{
String resp = response.asString();
JsonPath js = new JsonPath(resp);
return js.get(key).toString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想要一包所有变量,以后要保存和检索。我下面的POC仅适用于单线线程,不确定多个线程。
提取
或extractAndSave
因为我希望我的地图可以保存任何对象,因此我使用
map< string,object>
。它需要铸造以从地图中获取值,例如int key2_number =(integer)extractutils.extractfrom(res,“ data.key.2.number”);
I assume that you want something like a pack of all variables that you want to save and retrieve later. My POC below just work correctly for single thread, not sure about multiple threads.
extract
orextractAndSave
Because I want my map can save any object, so that I use
Map<String,Object>
. It requires casting to get value from the map, likeint key2_number = (Integer) ExtractUtils.extractFrom(res, "data.key2.number");