我试图在PHP的帮助下解析json值。 一切正常,但后续数组没有被打印出来,而是我得到了未定义的属性错误。我的第一个数组很容易显示。我关注
http://www.bewebdeveloper.com
但已根据我的要求进行了修改
请指出我在哪里犯了错误
这是我的 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提要
第二个标题
注意:未定义的属性:stdClass::$2 in C:''Program Files (x86)''EasyPHP-12.1''www''bobaloffers''json''json.php 在第 50 行
注意:未定义的属性:stdClass::$21 in C:''Program Files (x86)''EasyPHP-12.1''www''bobaloffers''json''json.php 在第 55 行 21
由 bewebdeveloper.com 提供支持
请指出防止后续错误被解析的原因
你的代码一团糟,我懒得重新缩进它,但点是切换到关联数组而不是对象可能解决了你所有的问题:
由于我不确定您要实现的目标,因此我只是将其更改为您的一对一代码,仅使用json_decode($var, true);
然后替换对解码 json 的所有引用。
?>
<!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 解析实现的是,我想要一个动态文件,在开发电子商务网站时对我很有用,我不需要将产品的所有规格功能等添加到数据库中,而是从 JSON 文件中获取完整的详细信息。
只需从顶部取下$posts = $jfo->result->$r;
并将其放在 for 循环中即可。
for($i=0; $i < $rt; $i++) {
$posts = $jfo->result->$r;
//rest codes
}
每次$r
值递增时,您都必须调用与$r
值对应的数组,但由于它不在循环内,我的代码试图在不递增到新值的情况下获取值,从而导致错误。