【发布时间】:2015-01-12 21:41:25
【问题描述】:
我试图在 php 的帮助下解析 json 值。一切正常,但后续数组没有打印,而是我收到未定义的属性错误。我的第一个数组很容易显示。正在关注
但已根据我的要求进行了修改
请指出哪里出错了
这是我的 json 文件
{
"result": {
"n":"2",
"title": "Rendering Json",
"10": "First Heading",
"1": [
{
"1": "How to Create an RSS Feed with PHP and MySQL",
"11": "XRT"
}
],
"20": "Second Heading",
"2": [
{
"2": "How to Create an RSS Feed with PHP and MySQL",
"21": "XRT",
"link": "http://www.bewebdeveloper.com/tutorial-about-how-to-create-an-rss-feed-with-php-and-mysql"
}
]
}
}
这是我的 php 代码
<?php
// copy file content into a string var
$json_file = file_get_contents('jso.txt');
// convert the string to a json object
$jfo = json_decode($json_file);
// read the title value
$title = $jfo->result->title;
// copy the posts array to a php var
$r=1;
$posts = $jfo->result->$r;
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>How to parse JSON file with PHP</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="header">
<img src="images/BeWebDeveloper.png" />
</div><!-- header -->
<h1 class="main_title"><?php echo $title; ?></h1>
<div class="content">
<table>
<?php
$rt=$jfo->result->n;
for($i=0; $i < $rt; $i++) {
$x=1;
?>
<th>
<?php
$ze=0;
$p=$r.$ze;
echo $jfo->result->$p;
?>
</th>
<?php
foreach ($posts as $post) {
$a=$i+1;
?>
<tr>
<td>
<h2><?php echo $post->$a; ?></h2>
</td>
<td>
<h2><?php
$z=$a.$x; echo $post->$z; ?></h2>
</td>
<td>
<?php
echo $a.$x;
$x++;
?>
</td>
</tr>
<?php
} // end foreach
$r++;
}
?>
</table>
</div><!-- content -->
<div class="footer">
Powered by <a href="http://www.bewebdeveloper.com">bewebdeveloper.com</a>
</div><!-- footer -->
</div><!-- container -->
</body>
</html>
这就是我的输出的样子
渲染 Json
第一个标题
如何使用 PHP 和 MySQL XRT 11 创建 RSS 源
第二个标题
注意:未定义的属性:第 50 行 C:\Program Files (x86)\EasyPHP-12.1\www\bobaloffers\json\json.php 中的 stdClass::$2
注意:未定义属性:C:\Program Files (x86)\EasyPHP-12.1\www\bobaloffers\json\json.php 中的 stdClass::$21 位于第 55 21 行
由 bewebdeveloper.com 提供支持
请指出是什么阻止了后续错误被解析
【问题讨论】:
-
那么第 50 行和第 55 行在哪里?
-
第 50 行:$a; ?>
-
第 55 行:$z=$a.$x;回声 $post->$z;
-
我已经提到我是一名 json 初学者,并且关注 bewebdeveloper.com 如果你能指导我而不是将我的问题标记为否定问题会更好
-
@developerwjk :如果您无法回答,请不要回答,无需将我的问题标记为否定问题。如果您是天才,那么如果您不能指导,那将毫无用处。看看这个空间我会回答我自己的问题