我正在从方法获得HTTPGET HTML响应。如何解析JSON的html响应?
我应该使用哪个API来解析它?
我还想知道x-path的主要用途是什么。HTML响应:
<!DOCTYPE html> <[if IE 8]> <html lang="en" class="ie ie8 lt-ie9 lt-ie10"> <![endif]> <[if IE 9]> <html lang="en" class="ie ie9 lt-ie10"> <![endif]> <if IE]> <html lang="en" class="ie"> <![endif] <[if gt IE 9]> <html lang="en"> <![endif] <head prefix="og: ogp.me/ns# fb: ogp.me/ns/fb# snapdeallog:#"> <link rel="dns-prefetch" href="i1.sdlcdn.com"; />发布于 2017-12-08 11:08:39
您可以使用汤汁作为工具将您的HTML转换为JSON。
final String HTML = "<!DOCTYPE html> <[if IE 8]> <html lang="en" class="ie ie8 lt-ie9 lt-ie10"> <![endif]> <[if IE 9]> <html lang="en" class="ie ie9 lt-ie10"> <![endif]> <if IE]> <html lang="en" class="ie"> <![endif] <[if gt IE 9]> <html lang="en"> <![endif] <head prefix="og: ogp.me/ns# fb: ogp.me/ns/fb# snapdeallog:#"> <link rel="dns-prefetch" href="i1.sdlcdn.com"; />";
Document document = Jsoup.parse(HTML);
Element element = document.select("ANY HTML TAG").first();
String arrayName = element.select("TAG2").first().text();
JSONObject jsonObj = new JSONObject();
JSONArray jsonArr = new JSONArray();
Elements ie = element.getElementsByClass("ie");
Elements ei9 = element.getElementsByClass("ei9");
JSONObject jobj = new JSONObject();
for (int i = 0, l = ie.size(); i < l; i++) {
String key = ie.get(i).text();
String value = ei9.get(i).text();
jobj.put(key, value);
}
jsonArr.put(jobj);
jsonObj.put(arrayName, jsonArr);
System.out.println(jsonObj.toString());https://stackoverflow.com/questions/47710939
复制相似问题