【问题标题】:Putting json data into table将json数据放入表中
【发布时间】:2013-08-06 07:40:27
【问题描述】:

所以我有一个 json 文件将其输入到表中。现在我想将价格发布到,但我不知道如何

这是我的 json:

    {
   "total": 1,
   "items": [
      {
         "icon": "http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=11230",
         "icon_large": "http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=11230",
         "id": 11230,
         "type": "Ammo",
         "typeIcon": "http://www.runescape.com/img/categories/Ammo",
         "name": "Dragon dart",
         "description": "A deadly throwing dart with a dragon tip.",
         "current": {
            "trend": "neutral",
            "price": 184
         },
         "today": {
            "trend": "neutral",
            "price": 0
         }
      }
   ]
}

现在我可以发布项目图标 ID 类型和名称。但我也希望发布当前价格。我已经尝试过以下方法:

    foreach($arr['current'] as $val2){
    echo '<td>'. htmlspecialchars($val2['price']) .'</td></tr>';          

}

出现以下错误:

警告:为 foreach() 提供的参数无效 第 28 行

这是我获取数据的脚本:

    <?php
//check if you have curl loaded
if(!function_exists("curl_init")) die("cURL extension is not installed");

$url = 'http://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?category=1&alpha=d&page=1';

$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);

$arr = json_decode($r,true);
echo "<table>";
foreach($arr['items'] as $val)
{
    echo '<tr>';
    echo '<td>'.$val['id'].'</td>';
    echo '<td>'. htmlspecialchars($val['type']) .'</td>';
    echo '<td>'. htmlspecialchars($val['name']) .'</td>';
    echo '<td><img src='. $val['icon'].'></td>'; 

}
foreach($arr['current'] as $val2){
    echo '<td>'. htmlspecialchars($val2['price']) .'</td></tr>';          

}
echo "</table>";

?>

所以我的问题是:我如何也发布当前价格?

【问题讨论】:

    标签: php html json curl


    【解决方案1】:

    最好写成

     echo '<td>'. htmlspecialchars($val['current']['price']) .'</td></tr>';     
    

    您正在尝试迭代 $arr 这是不包含名为 current 的元素的外部数组

    【讨论】:

      【解决方案2】:
      $json_str='   {
         "total": 1,
         "items": [
            {
               "icon": "http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=11230",
               "icon_large": "http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=11230",
               "id": 11230,
               "type": "Ammo",
               "typeIcon": "http://www.runescape.com/img/categories/Ammo",
               "name": "Dragon dart",
               "description": "A deadly throwing dart with a dragon tip.",
               "current": {
                  "trend": "neutral",
                  "price": 184
               },
               "today": {
                  "trend": "neutral",
                  "price": 0
               }
            }
         ]
      }';
      $arr = json_decode($json_str,true);
      // this $arr contains array with-in array so there is no direct access to 
      // 'Price' its either inside current or today so access  them accordingly like 
      $arr['current']['price'];
      //or
      $arr['today']['price'];
      

      【讨论】:

        【解决方案3】:

        如果 current 内部的属性并不总是返回相同的键,你可以使用这个:

        if(!function_exists("curl_init")) die("cURL extension is not installed");
        
        $url = 'http://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?category=1&alpha=d&page=1';
        
        $ch=curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $r=curl_exec($ch);
        curl_close($ch);
        
        $arr = json_decode($r,true);
        
        echo "<table border='1'>";
        foreach($arr['items'] as $val)
        {
            echo '<tr>';
            echo '<td>'.$val['id'].'</td>';
            echo '<td>'. htmlspecialchars($val['type']) .'</td>';
            echo '<td>'. htmlspecialchars($val['name']) .'</td>';
            echo '<td><img src='. $val['icon'].'></td>';
            foreach($val['current'] as $v){
                echo '<td>'. htmlspecialchars($v) .'</td>';
            }
        }
        echo "</table>";
        

        【讨论】:

          【解决方案4】:

          试试这个。

          首先检查数组是否为空。如果数组为空,则会出错,因为您传递的是关联数组,并且在第一个循环中没有关闭 tr 标签。

          if(!empty($arr))
          {
               //for loop
          }
          else
          {
              echo "Array empty";
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-05-13
            • 2011-02-13
            • 2018-04-04
            • 2011-11-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多