使json响应中的HTML标记正常工作


make HTML tags inside json response work

我正在使用维基百科API作为JSON从维基百科检索一些数据。我得到了html标签和数据作为响应。当在浏览器上显示时,html标签显示为纯文本,而不是常规输出(我希望我清楚这句话)。我想展示一下它应该是什么样子,而不是纯文本。下面是我的PHP代码。

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Sachin_Tendulkar&format=json&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
echo $json ;

以下是angularJS控制器,用于显示数据

var demoApp = angular.module('demoApp',[]);
demoApp.controller('SimpleController',function ($scope,$http){
$http.post('server/view1.php').success(function(data){
    $scope.info = data;
});
});

在这种情况下:

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts&titles=Sachin_Tendulkar&format=json&redirects&inprop=url&indexpageids';
$jsonString = file_get_contents( $url );
$jsonDecoded = json_decode($jsonString, true );
$pageData = $jsonDecoded['query']['pages'][57570]['extract'];
echo $pageData;

如果您想要解释json数据,则需要使用函数json_decode。

您必须使用json_decode($json)来解析对有效php代码的响应。