php - 如何用php将json数据转成html?

标签 php json

如何用php将json数据转成html?

$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"

当我在浏览器中输入 url 时,它返回 {"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]} 如何放置 json body数据转换成html?我的意思是这样echo '<div class="body"></div>';

最佳答案

您首先需要获取文件。你应该为此使用 curl。在下面的示例中,我使用了 file_get_contents() 函数,您可能想要替换它。使用 json_decode() 为您解析 JSON。

$json = file_get_contents($url);
$data = json_decode($json);
echo $data->results[0]->body;

这将回应A guide to cultural...

关于php - 如何用php将json数据转成html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4634407/

相关文章:

php - mysql 按非常大的数字对行进行排序。加载时间

php - 如何在 SQL 命令中使用两个 where 语句?

php - 谷歌 API 客户端 :"{ "错误“: "invalid_grant", "error_description": "Incorrect token type."}”

php - 无法连接到 godaddy 本地主机

php - 使用 filter_var 清理字符串

javascript - 在 jQuery $.each 中访问 JSON 中的多个对象/数组

javascript - 一个用于解析糟糕 JSON 的库 - 它存在吗?

android - JSON数组在android中自动完成 TextView

java - 将 JSON 数据解析为 map 标记,所有标记显示相同的信息,我该如何解决这个问题?

python - JSON 文件到 Pandas df