java - 我需要从列表标签内的 json 脚本获取数据

标签 java json selenium

我需要在 li 标记内获取 Json 变量值,例如 numberOfRecommendations ,即 3

<li data-extra="{&quot;hook&quot;:&quot;LFWB Hypertension&quot;,&quot;text&quot;:&quot;This patient does not have Diabetes.  There we can treat with a calcium-channel blocker.&quot;,&quot;type&quot;:&quot;detail&quot;,&quot;source&quot;:{&quot;label&quot;:&quot;{\&quot;orgName\&quot;:\&quot;Liver Foundation\&quot;,\&quot;cdsCardId\&quot;:3,\&quot;cardName\&quot;:\&quot;This patient does not have Diabetes.  There we can treat with a calcium-channel blocker.\&quot;,\&quot;author\&quot;:\&quot;Dr. Abhishek Das\&quot;,\&quot;ifRecommendedByCurrentUser\&quot;:true,\&quot;ruleName\&quot;:\&quot;Systolic Blood Pressure greater than 140 without Diabetes History\&quot;,\&quot;serviceName\&quot;:\&quot;LFWB Hypertension\&quot;,\&quot;numberOfRecommendations\&quot;:3,\&quot;uuid\&quot;:\&quot;219470ab-37a9-4fac-94b0-09cb34fb4e19\&quot;}&quot;}}" style="" xpath="1">This patient does not have Diabetes.  There we can treat with a calcium-channel blocker.<br><label style="color: white; font-weight: normal;">aaaa</label><label style="color: grey; float: right; font-weight: normal;">Dr. Abhishek Das, LFWB Hypertension</label><label style="color: white; font-weight: normal; float: right;">---</label><label style="color: rgb(30, 136, 229); float: right; font-weight: normal;">3  </label><img src="resources/images/headshots/recommended.png" width="15" height="15" style="border-radius: 50%; display: inline-block; margin-right: 5px; float: right;"></li>

最佳答案

请找到下面的代码,这将帮助您获取属性“data-extra”,并从中提取您想要的任何字段。

第 1 步: 获取包含 JSON 响应的 data-extra 字段。

String jsonStr = driver.findElement(By.tagName("li")).getAttribute("data-extra");
System.out.println(jsonStr);

第 2 步: 现在我们需要清理 JSON 响应,因为它包含额外的反斜杠和双引号。

String jsonFormattedString = jsonStr.replaceAll("\\\\", "");
jsonFormattedString = jsonFormattedString.replace("\"{", "{");
jsonFormattedString = jsonFormattedString.replace("\"}}", "}}");
System.out.println(jsonFormattedString);

第 3 步: 现在我们需要解析 JSON 并从 JSON 响应中提取 numberOfRecommendations 键字段。

JSONObject jsonObject = new JSONObject(jsonFormattedString);
JSONObject source = jsonObject.getJSONObject("source");
JSONObject label = source.getJSONObject("label");
int numberOfRecommendations = label.getInt("numberOfRecommendations");
System.out.println(numberOfRecommendations);

注意:由于您没有提到完整的HTML,我假设tagName字段为“li” ”。

关于java - 我需要从列表标签内的 json 脚本获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57653746/

相关文章:

javascript - 通过 Selenium Webdriver 验证 JavaScript 错误

javascript - Selenium webdriver element.click() 未按预期方式运行(chrome、mocha)

java - Android 上 Java 中的文件路径

java - listView的每个项目都播放一个不同的(简短的)媒体文件。剪辑了那么多之后,我听不到声音了

java - 如何创建基于 JavaScript 数组的 Java 列表?

python - 在python中将一个json字典添加到另一个json字典中

java - SecurityError : CSSStyleSheet. cssRules getter:使用 Selenium Firefox 读取 CSS 样式表时不允许访问跨源样式表错误

java - 字符串未写入新行

java - Android 权限没有执行任何操作

javascript - 如何使用 javascript 正则表达式替换 json 键值(当 json 无效时)?