【发布时间】:2019-03-03 04:33:03
【问题描述】:
我正在使用 Java 1.7...
具有以下 JSON 响应(来自 JSON 数组),其中包含不同的标签(一个包含照片,另一个包含视频):
{
"articles":
[
{
"htmlBody": "<asset-entity type=\"photo\" id=\"4806ad76-7433-fs34-50d1-b12bdbc308899ad\"></asset-entity>\r\nAngelie Jolie was seen at Wholefoods with ex-beau Brad Pitt.\r\n
<asset-entity type=\"photo\" id=\"4806fe7d-c175-c380-4ab2-dda068b42b033cbf\"></asset-entity>\r\n- The majority of their kids were with them.\r\n<asset-entity type=\"photo\" id=\"35064086-5d85-1866-4afc-a523c04c2b3e42a6\">
</asset-entity>\r\n"
},
{
"htmlBody": "<asset-entity type=\"video\" id=\"48906fe30-8dx6-7e04-4b18-98c4d77176eaz412\"></asset-entity>\r\n
Reese Witherspoon was spotted at the Paris airport\n\nRumor is that she arrived for the filming of her new movie\n\n <asset-entity type=\"video\" id=\"4207182e-cgga-4e0a-4b97-a5ec0aa619c33b42\"></asset-entity>\r\n"
},
{
"htmlBody": "<asset-entity type=\"photo\" id=\"350686a2-6fef-9fd7-445d-b2888fa56c3454da\"></asset-entity>\r\nMatt Damon was seen walking to StarBucks for a quick latte and chocalate danish while in Boston.\r\nHere's a video clip of him kindly greeting the paparazzi:<asset-entity type=\"video\" id=\"2507f140-ed4c-7e1b-4f44-8c57e051409d6fea\"></asset-entity>\r\n"
}
]
}
在我的 Java 代码中,htmlBody 是一个字符串...
问题:
-
谁能给我一个好的正则表达式查询(使用Java)来解析:
和:
<asset-entity type=\"photo\" id=\"48906fe30-8dx6-7e04-4b18-98c4d77176eaz412\"></asset-entity>
希望从照片或视频中提取 id 并存储到数据结构(例如 HashMap)中,但需要能够找到一种机制,该机制将在我的代码中使用正则表达式搜索基于字符串的 htmlBody 以查找照片和视频.
一旦我将 id 存储在正确的数据结构中:
例如
Map<String> videoTags = new HashMap();
Map<String> photoTags = new HashMap();
然后,我可以将这些标签替换为实际的(或包含实际资产的等效标签。
- HashMap 是存储这些特定资产 ID 的最佳方式(有意或用实际资产的硬编码 URL 替换它们)?
任何关于正则表达式或设计的建议都将不胜感激......如果正则表达式不是在 Java 中搜索特定自定义 HTML 标记(作为字符串)的可行解决方案,我还能使用什么(在技术方面) ?
【问题讨论】:
-
@Tim - 所以你是说除了使用正则表达式之外没有其他方法可以搜索这两个不同的自定义 HTML 标签?感谢您的回复。
-
如果id的长度相同,为什么不直接找到“id”的索引然后提取它
-
Issam - 我需要知道它属于视频还是照片 - 这就是问题所在?提取后我会将数据放在哪里(例如数据结构)?
-
我不确定您是如何得出这个结论的。这根本不是我要说的,实际上我只分享了一个链接,该链接中的答案说 not 使用正则表达式来处理 html。