简体   繁体   English

将php json转换为javascript getJSON

[英]Converting php json to javascript getJSON

Hello stackoverflow community, i'm apologising for my ignorance in javascript/ajax but i have a hard time to convert this php json into a javascript function 你好stackoverflow社区,我为我在javascript / ajax中的无知道歉,但我很难将这个php json转换为javascript函数

$json =    file_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json');
$json_a = json_decode($json,true);
$url = $json_a[videos][0][url];
$img = $json_a[meta][poster];
echo $url;
echo $img;

Thanks in advance for any help given 提前感谢您提供的任何帮助

Var_dump Json Var_dump Json

string(984) "{"version":3,"service":"mail","provider":"ugc","author":{"email":"alex.costantin@mail.ru","name":"alex.costantin","profile":"http://my.mail.ru/mail/alex.costantin"},"meta":{"title":"avg","externalId":"mail/alex.costantin/_myvideo/4375","itemId":4375,"accId":54048083,"poster":"http://videoapi.my.mail.ru/file/sc03/2500725436577747223","duration":7955,"url":"http://my.mail.ru/mail/alex.costantin/video/_myvideo/4375.html","timestamp":1430140403,"viewsCount":13345},"videos":[{"key":"360p","url":"http://cdn28.my.mail.ru/v/54048083.mp4?sign=dab566053f09db40a63a263f17190aeeb09f1d8d&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-v.mp4&p=f&expire_at=1430773200&touch=1430140403","seekSchema":3},{"key":"720p","url":"http://cdn28.my.mail.ru/hv/54048083.mp4?sign=e9ea54e857ca590b171636efae1b80ccdf0bb5bf&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-hv.mp4&p=f&expire_at=1430773200&touch=1430140403","seekSchema":3}],"encoding":true,"flags":16387,"spAccess":3,"region":"200"}" 

Var_dump Json_a Var_dump Json_a

array(10) { ["version"]=> int(3) ["service"]=> string(4) "mail" ["provider"]=> string(3) "ugc" ["author"]=> array(3) { ["email"]=> string(22) "alex.costantin@mail.ru" ["name"]=> string(14) "alex.costantin" ["profile"]=> string(37) "http://my.mail.ru/mail/alex.costantin" } ["meta"]=> array(9) { ["title"]=> string(3) "avg" ["externalId"]=> string(33) "mail/alex.costantin/_myvideo/4375" ["itemId"]=> int(4375) ["accId"]=> int(54048083) ["poster"]=> string(56) "http://videoapi.my.mail.ru/file/sc03/2500725436577747223" ["duration"]=> int(7955) ["url"]=> string(62) "http://my.mail.ru/mail/alex.costantin/video/_myvideo/4375.html" ["timestamp"]=> int(1430140403) ["viewsCount"]=> int(13345) } ["videos"]=> array(2) { [0]=> array(3) { ["key"]=> string(4) "360p" ["url"]=> string(185) "http://cdn28.my.mail.ru/v/54048083.mp4?sign=dab566053f09db40a63a263f17190aeeb09f1d8d&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-v.mp4&p=f&expire_at=1430773200&touch=1430140403" ["seekSchema"]=> int(3) } [1]=> array(3) { ["key"]=> string(4) "720p" ["url"]=> string(187) "http://cdn28.my.mail.ru/hv/54048083.mp4?sign=e9ea54e857ca590b171636efae1b80ccdf0bb5bf&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-hv.mp4&p=f&expire_at=1430773200&touch=1430140403" ["seekSchema"]=> int(3) } } ["encoding"]=> bool(true) ["flags"]=> int(16387) ["spAccess"]=> int(3) ["region"]=> string(3) "200" } 

So far i've made this but no success, what's wrong with the code? 到目前为止,我已经做了这个但没有成功,代码有什么问题?

$.ajax({ type: "GET", url: "http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json", async: false, beforeSend: function(x) { if(x && x.overrideMimeType) { x.overrideMimeType("application/j-son;charset=UTF-8"); } }, dataType: "json", success: function(data){ alert(data.meta.poster); }});

If you can't get it thru JSONP, you could just create that PHP wrapper that handles the request, then call that PHP url of yours to get it. 如果你无法通过JSONP获得它,你可以创建处理请求的PHP包装器,然后调用你的PHP url来获取它。

Here's a rough example on the same page (of course, it would be much better if you separate the PHP file). 这是同一页面上的一个粗略示例(当然,如果将PHP文件分开,会更好)。

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['json_call'])) {
    echo file_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json');
    exit;
}

?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
    url: document.URL,
    dataType: 'JSON',
    type: 'POST',
    data: {json_call : true},
    success: function(response) {
        alert(response.version);
        alert(response.videos[0].key);
    }
});
</script>

Sample Output 样本输出

I think its just the way you are accessing the variables, you need to use quotes around your keys 我认为只是你访问变量的方式,你需要在键周围使用引号

<?php
   $json = '{"version":3,"service":"mail","provider":"ugc","author":{"email":"alex.costantin@mail.ru","name":"alex.costantin","profile":"http://my.mail.ru/mail/alex.costantin"},"meta":{"title":"avg","externalId":"mail/alex.costantin/_myvideo/4375","itemId":4375,"accId":54048083,"poster":"http://videoapi.my.mail.ru/file/sc03/2500725436577747223","duration":7955,"url":"http://my.mail.ru/mail/alex.costantin/video/_myvideo/4375.html","timestamp":1430140403,"viewsCount":13345},"videos":[{"key":"360p","url":"http://cdn28.my.mail.ru/v/54048083.mp4?sign=dab566053f09db40a63a263f17190aeeb09f1d8d&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-v.mp4&p=f&expire_at=1430773200&touch=1430140403","seekSchema":3},{"key":"720p","url":"http://cdn28.my.mail.ru/hv/54048083.mp4?sign=e9ea54e857ca590b171636efae1b80ccdf0bb5bf&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-hv.mp4&p=f&expire_at=1430773200&touch=1430140403","seekSchema":3}],"encoding":true,"flags":16387,"spAccess":3,"region":"200"}';

  $json_a = json_decode($json,true);

  var_dump($json_a["meta"]["poster"]); 
  //string(56) "http://videoapi.my.mail.ru/file/sc03/2500725436577747223"
?>

to set a js variable with your php you can do this: 用你的php设置一个js变量你可以这样做:

<script>
    var poster = '<?php echo $json_a["meta"]["poster"]; ?>';
</script>

to set a js object with your php you can do this: 用你的php设置一个js对象,你可以这样做:

<script>
    var jsonString = '<? phpfile_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json'); ?>';
    var jsonObj = JSON.parse(jsonString);
</script>

You can use the Simple JSON for PHP library to forge your complex JSON and merge multiple json together without decoding them. 您可以使用Simple JSON for PHP库来伪造复杂的JSON并将多个json合并在一起而不解码它们。

<?php

  include('../includes/json.php');

  // $json = new json(); // Pure JSON
  $json = new json('callback', 'myCallback'); // JSON with Callback

  $jsonOnly = file_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json');

  $json->add('status', '200');

  if(connected){
    $json->add("alex.constantin", $jsonOnly, false);
    $json->add("authorized", true);
    // $json->add("authorized"); can also be used
  }
  else 
    $json->add("authorized", false);

  $json->send();
?>

In your HTML you can mainly call it via 2 ways : 在您的HTML中,您可以主要通过两种方式调用它:

Legacy JS & DOM elements. 传统的JS和DOM元素。

The callback must be "integrated" with something like : mycallback({ ... }); 回调必须与以下内容“集成”: mycallback({ ... });

function load_script(url) {
  var s = document.createElement('script'); 
  s.src = url;
  document.body.appendChild(s);
}

function load_scripts() {
  load_script('myPhpPage');
}

window.onload=load_scripts;

Ajax via Legacy JS or via JQuery Ajax通过Legacy JS或通过JQuery

The callback should be like : {} and is called so : 回调应该像: {}并且被调用为:

$.getJSON('http://example.com/MyPHP.php',
data,
function(json) {
  alert(json);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM