【问题标题】:parsing json gives undefined property error解析 json 给出未定义的属性错误
【发布时间】:2015-01-12 21:41:25
【问题描述】:

我试图在 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 源

第二个标题

注意:未定义的属性:第 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 :如果您无法回答,请不要回答,无需将我的问题标记为否定问题。如果您是天才,那么如果您不能指导,那将毫无用处。看看这个空间我会回答我自己的问题

标签: php json


【解决方案1】:

您的代码一团糟,我懒得重新缩进它,但重点是切换到关联数组而不是对象可能会解决您的所有问题:

由于我不确定您到底想要实现什么,我只是将其更改为您的一对一代码,只需使用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>

【讨论】:

    【解决方案2】:

    我自己解决了。我想通过我的 JSON 解析实现的是,我想要一个对我有用的动态文件,同时开发一个电子商务网站,我不需要将产品的所有规格功能等添加到数据库中,而是从 JSON 中获取完整的详细信息文件。

    只需从顶部删除 $posts = $jfo-&gt;result-&gt;$r; 并将其放入 for 循环中。

     for($i=0; $i < $rt; $i++) {
    
     $posts = $jfo->result->$r;
    
       //rest codes
       }
    

    每次$r 值递增时,您必须调用与$r 值对应的数组,但由于它不在循环内,我的代码试图获取值而不递增到新值,因此出现错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 2014-08-15
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      相关资源
      最近更新 更多