如何显示此json数据中的变量

时间:2013-08-24 23:40:16

标签: php json curl jsonp

所以我试图让这个工作无济于事。下面是我的PHP文件和我的JSON数据。

注意我必须先将JSONP转换为JSON。这工作得很好。现在看看这个疯狂的数据数组,我不知道如何在foreach循环中获取变量。任何帮助都会有很好的帮助。

由于

MYFILE.php

$url = "http://www.SwingAcademy.com/web/external/api.ashx?method=getLessons&     callback=?";  
$groupID = "43AB3367-F58D-487E-B2C6-2571A3A3508C";
$num_results = "";

$post_data = array ( 
    "NUMRESULTS" => $num_results,
    "GROUPID" => $groupID,
    "OWNERID" => ''

);  


$curl_options = array(
    CURLOPT_URL => $url ,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $post_data ),
    CURLOPT_HTTP_VERSION => 1.0,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
);

$curl = curl_init();

curl_setopt_array( $curl, $curl_options );

$result = curl_exec( $curl );

//echo $result;

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter   to json_decode
   if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
       $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    return json_decode(trim($jsonp,'();'), $assoc);
}
echo "<pre>";
var_dump(jsonp_decode($result, true));
echo "</pre>";

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
   }

   echo PHP_EOL;

   foreach($result['VIDEOS'] as $p)
   {
     echo '

     Lesson Info: '.$p['LESSONTEXT'].' '.$p['LESSONID'].'

     Image Link: '.$p['THUMBNAIL'].'

     ';
   }

我的返回JSON

array(3) {
  ["frame_size"]=>
  array(2) {
    ["width"]=>
    int(800)
    ["height"]=>
    int(470)
  }
  ["num_results"]=>
  int(9)
  ["VIDEOS"]=>
  array(9) {
    [0]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(29) "test again... : test again..."
      ["LESSONID"]=>
      string(4) "4877"
      ["INSTRUCTORID"]=>
      string(3) "193"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(3) "193"
      ["LESSONDATE"]=>
      string(10) "1376601089"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages /apiPlayer.aspx?l=21E7D7B4-822D-4693-8C66-62C759D669E5"
      ["THUMBNAIL"]=>
      string(90) "http://www.swingacademy.com/useravatars/SA/SKILLCAST_SNAP/000001-100000/4001-5000/4877.jpg"
    }
    [1]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(25) "test lesson : test lesson"
      ["LESSONID"]=>
      string(4) "4876"
      ["INSTRUCTORID"]=>
      string(3) "193"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(3) "193"
      ["LESSONDATE"]=>
      string(10) "1376601043"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=BE475D77-BAD8-4460-8729-83AE67220540"
      ["THUMBNAIL"]=>
      string(90) "http://www.swingacademy.com/useravatars/SA/SKILLCAST_SNAP/000001-100000/4001-5000/4876.jpg"
    }
    [2]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(14) "test : testing"
      ["LESSONID"]=>
      string(4) "4875"
      ["INSTRUCTORID"]=>
      string(3) "193"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(3) "193"
      ["LESSONDATE"]=>
      string(10) "1376598912"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=C9755920-9B7D-46D3-97DE-E52DA41966DC"
      ["THUMBNAIL"]=>
      string(90) "http://www.swingacademy.com/useravatars/SA/SKILLCAST_SNAP/000001-100000/4001-5000/4875.jpg"
    }
    [3]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(16) "this is a test :"
      ["LESSONID"]=>
      string(4) "4874"
      ["INSTRUCTORID"]=>
      string(4) "3795"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3795"
      ["LESSONDATE"]=>
      string(10) "1375361193"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=5B3E4DE3-AD09-4FEE-B111-937D4664B844"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [4]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(50) "First review of myself : my first review of myself"
      ["LESSONID"]=>
      string(4) "4872"
      ["INSTRUCTORID"]=>
      string(5) "77360"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(5) "77360"
      ["LESSONDATE"]=>
      string(10) "1375306891"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=0FDBE5F0-0A6E-43FF-A4CB-4320D2E6C043"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [5]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(25) "test record : test record"
      ["LESSONID"]=>
      string(4) "4871"
      ["INSTRUCTORID"]=>
      string(4) "3795"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3795"
      ["LESSONDATE"]=>
      string(10) "1374257704"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=FEA838AF-BB74-4478-9773-7CD3F160DF83"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [6]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(8) "Test 1 :"
      ["LESSONID"]=>
      string(4) "4870"
      ["INSTRUCTORID"]=>
      string(4) "9924"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "9924"
      ["LESSONDATE"]=>
      string(10) "1373540666"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=8AD8B9A1-DDA6-4E89-A9A6-2805988FB758"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [7]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(9) "testing :"
      ["LESSONID"]=>
      string(4) "4869"
      ["INSTRUCTORID"]=>
      string(4) "3788"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3788"
      ["LESSONDATE"]=>
      string(10) "1373321906"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=A3F34283-00C4-47F0-951A-BD0E44C3E9F5"
      ["THUMBNAIL"]=>
      string(0) ""
    }
    [8]=>
    array(8) {
      ["LESSONTEXT"]=>
      string(39) "this is a test analysis : test analysis"
      ["LESSONID"]=>
      string(4) "4868"
      ["INSTRUCTORID"]=>
      string(4) "3788"
      ["NOTE"]=>
      string(21) "I analyzed your video"
      ["SHAREDBY"]=>
      string(4) "3788"
      ["LESSONDATE"]=>
      string(10) "1373321858"
      ["VIDEOURL"]=>
      string(100) "http://www.swingacademy.com/web/external/pages/apiPlayer.aspx?l=34DB255C-5C12-4A15-B09B-0F734F0032E9"
      ["THUMBNAIL"]=>
      string(0) ""
    }
  }
}

- No errors
Warning: Invalid argument supplied for foreach() in

要注意这是为JavaScript构建的,但我需要将它与PHP一起使用。所以我做了所有的转换和处理。我现在可以知道我有这些数据如何在foreach循环中显示它并回显变量。

这些数据的存储方式看起来很陌生。

2 个答案:

答案 0 :(得分:2)

echo "<pre>";
var_dump(jsonp_decode($result, true));
echo "</pre>";
$arrResult = jsonp_decode($result, true);
.
.
.
foreach($arrResult['VIDEOS'] as $p)

答案 1 :(得分:1)

在使用之前,您需要将jsonp_decode的结果分配给变量$result

$result = jsonp_decode($result, true);