如何从json回调中获取特定单词

时间:2016-02-21 21:06:13

标签: javascript json ajax

在extern服务器上我有这个json文件:

{"contents":"\n<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body>\n<strong>Racex8cr</strong></body>\n</html>\n\n","status":{"url":"http://****.nl/","content_type":"text/html; charset=UTF-8","http_code":200}}

使用Javascript(在我的localhost上)我想在此json文件中获取<strong>元素的内容,然后使用console.log显示以下文本行:

Racex8cr

有人能给我一些例子我应该怎么做?

1 个答案:

答案 0 :(得分:-1)

考虑到您将JSON存储在名为response的变量中,您可以使用JavaScript的String.prototype.search函数。

var strongText = response.contents.substring((response.contents.search("<strong>") + "<strong>".length), response.contents.search("</strong>"));

&#13;
&#13;
//JSON won't parse correctly, this is a simulation of JSON actually parsing:
var response = {
  contents: "\n<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body>\n<strong>Racex8cr</strong></body>\n</html>\n\n"
};

var strongText = response.contents.substring((response.contents.search("<strong>") + "<strong>".length), response.contents.search("</strong>"));
document.getElementById("output").innerHTML = strongText;
&#13;
And the strong text is: <b id='output'>loading...</b>
&#13;
&#13;
&#13;

相关问题