简体   繁体   English

将JSON响应解析为PHP

[英]Parsing a JSON response into PHP

I am trying to parse a JSON response by using some PHP but I am struggling with getting the issues URL and Title to display. 我正在尝试通过使用一些PHP来解析JSON响应,但是我正努力使问题URL和标题显示出来。

I have tried this code with a different JSON Request and it works fine. 我已经尝试使用其他JSON请求使用此代码,并且效果很好。

The problem is that nothing is displayed. 问题是什么都不显示。

Any help with this would be great. 任何帮助都会很棒。

<div style="float:left;border:10px solid #f5f5f5;width:90%;margin:20px;padding:20px;">
<?php
$jsondata = file_get_contents ("http://www.joomag.com/Frontend/WebService/restAPI.php?action=listIssues&key=api_9c63e3bd91c677ae9584da37acdbe501&magazine_ID=M0362553001407761640");

$json = json_decode($jsondata,true);
$output = "<ul>";
foreach($json['issues'] as $issue) {

$output .= "<li><a href=".$issues['url'].">".$issues['title']."</a></li>";


}

$output .= "</ul>";
echo $output;

?>

</div>

Here is the JSON 这是JSON

{
error: 0,
msg: "",
response: {
issues: [
{
title: "Gridline Spring 2014 Issue 2",
volume: "Issue 2",
ID: "0083637001411719447",
pages: 20,
url: "http://www.joomag.com/magazine/mag/0083637001411719447",
cover: "http://s1.joomag.com/res_mag/0/172/172900/335523/thumbs/7930027.jpg",
desc: "This is the description of issue 2",
privacy: "unlisted"
},
{
title: "Gridline Spring 2014 Spring 2014",
volume: "Spring 2014",
ID: "0171323001407761665",
pages: 20,
url: "http://www.joomag.com/magazine/mag/0171323001407761665",
cover: "http://s1.joomag.com/res_mag/0/172/172900/306732/thumbs/7174024.jpg",
desc: "Gridline Spring 2014",
privacy: "unlisted"
}
]
}
}

You have to refer to $issue instead of $issues in the loop. 您必须在循环中引用$issue而不是$issues Also the array to loop over is $json['response']['issues'] instead of $json['issues'] . 同样,要循环的数组是$json['response']['issues']而不是$json['issues']

Do like 喜欢

$jsondata = file_get_contents ("http://www.joomag.com/Frontend/WebService/restAPI.php?action=listIssues&key=api_9c63e3bd91c677ae9584da37acdbe501&magazine_ID=M0362553001407761640");
$json = json_decode($jsondata,true);
$output = "<ul>";
foreach($json['response']['issues'] as $issue) {

$output .= "<li><a href=".$issue['url'].">".$issue['title']."</a></li>";


}
$output .= "</ul>";
echo $output;

try this 尝试这个

echo "<pre>";
print_r($json['response']['issues'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM