【问题标题】:Error while printing JSON data in PHP?在 PHP 中打印 JSON 数据时出错?
【发布时间】:2014-08-28 07:25:31
【问题描述】:

我正在 PHP 中对 Web 服务执行 HTTP GET 请求。我收到 JSON 格式的响应。 但是在打印来自 JSON 的一些值(如 inkey2 和 key3)时,我遇到了一些错误。 我使用的 GET 请求是正确的。也没有语法错误。即使我在问题中指定的 URL 有点假,URL 也是正确的。 :)

我收到的作为 HTTP GET 请求响应的 JSON

{
   "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 请求

$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;
}

为什么我无法打印 key3 和 inkey2 的内容???

【问题讨论】:

    标签: php json foreach


    【解决方案1】:

    你应该使用:

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

    这部分很好,可以在您的代码中使用。

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

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      只需调用一个javascript函数,该函数就会使用ajax lik将corrdiantes传递给PHP脚本

      <script>
      function sendCoords(_lat,_long){
        $.ajax({
      url: "yourdomain.com/coords.php",
      data:{lat:_lat,long:_long}
      });
      }
      </script>
      

      您的 coords.php 脚本应如下所示:

      $lat = $_REQUEST['lat'];
      $lang = $_REQUEST['long'];
      ///Do something
      

      【讨论】:

        【解决方案3】:

        这很好用:

        foreach($printinkey2 as $key=>$value)
        {
            print $key." @sunny ".$value['@sunny'].PHP_EOL;
            print $key." @leone ". $value['@leone'].PHP_EOL;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-08-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多