duanjian5059 2014-08-28 07:25
浏览 81
已采纳

在PHP中打印JSON数据时出错?

I am performing a HTTP GET request in PHP to a webservice. I am getting response as JSON. But I faced a few errors while printing some of the values from JSON like inkey2 & key3. The GET request that I used is correct. There are no syntax errors as well. URL is correct even though the URL that I specified here in the question is a bit fake. :)

The JSON that I got as response for the HTTP GET request

{
   "name":
   {
       "key1": "salala",          
       "key2":
       {              
           "inkey1": "hike",
           "inkey2":
           [
               {
                   "@sunny": "fake",
                   "@leone": "take"
               },
               {
                   "@sunny": "make",
                   "@leone": "bake"
               },
               {
                   "@sunny": "cake",
                   "@leone": "drake"
               },
               {
                   "@sunny": "sake",
                   "@leone": "lake"
               }
          ],
           "inkey3": "bike"
      },
      "key3":
       [
           "batman",
           "superman",
           "spiderman",
           "ironman",
           "hancock"
       ],
       "key4": "nike"
   }
}

//HTTP GET request

$url='iwonttellyaguys.com';

$jsondata= httpGet($url);
$val_array = json_decode($jsondata, true);

echo'</br>';
echo'</br>';

//To print value salala (successful)

$print_salala=$val_array['name']['key1'];

echo'</br>';
echo'</br>';

//To print value hike & bike (successful)

$print_hike=$val_array['name']['key2']['inkey1'];
$print_bike=$val_array['name']['key2']['inkey3'];

echo'</br>';
echo'</br>';

//To print contents of key4. i.e, to print value nike (successful)

$print_nike=$val_array['name']['key4'];

echo'</br>';
echo'</br>';

//To print contents of inkey2 (Error)
//Incorrect value being printed.

$print_inkey2=$val_array['name']['key2']['inkey2'];

foreach($print_inkey2 as $key=>$value)
       {
          $sunny_leone=$value['@sunny']['@leone'];
          echo $sunny_leone;
       }
This foreach loop returns 3 as output...I am trying to print the contents, not the count.


//To print contents of key3 (Error)
//Incorrect value being printed.

echo'</br>';
echo'</br>';

$hero=$val_array['name']['key3'];
$count_hero=count($hero);

for($i=0;$i<$count_hero;$i++)
{
  echo $hero[$i];
}


// Function Definition of httpGet

function httpGet($url)
{
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    // curl_setopt($ch,CURLOPT_HEADER, false);

    $output=curl_exec($ch);
    curl_close($ch);
    return $output;
}

Why am I not being able to print contents of key3 & inkey2 ???

  • 写回答

3条回答 默认 最新

  • duanhuokuang5280 2014-08-28 07:42
    关注

    you should use:

    $print_inkey2=$val_array['name']['key2']['inkey2'];
    
    foreach($print_inkey2 as $key=>$value)
    {
        $sunny_leone=$value['@sunny'].$value['@leone'];
        echo $sunny_leone;
    }
    

    this part is fine and working in your code itself.

    $hero=$val_array['name']['key3'];
    $count_hero=count($hero);
    
    for($i=0;$i<$count_hero;$i++)
    {
      echo $hero[$i];
    }
    

    hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line