Skip to main content
4 de 5
se añadieron 2600 caracteres en el cuerpo
Renzo
  • 35
  • 10

Como acceder a un objeto en un JSON para luego acceder a un campo en uno de sus arrays

Quiero acceder al campo source que está dentro del array media, dentro de "data"

Recogiendo por api el JSON tengo esto :

$resp =  $client->call("List", array("json_access" => json_encode($access), "json_media"=>json_encode($filters)));
$variable = file_get_contents($resp,true);
$decoded = json_decode($variable);

EDIT :

Al hacer var_code($decoded) me lanza esto:

Warning: file_get_contents({"status":"success","error":"0","data":{"media":[{"id":"1552057437978","name":"videoCorto.mp4","original_name":"videoCorto.mp4","file":"3423e47523e07d4a62731f6506c1f64b.mp4","src":"http:\/\/mediaexample.net\/ec\/3423e47523e07d4a62731f6506c1f64b.mp4","origin":"nfs","uploaded":"2019-03-08 15:03:57","modified":"2019-03-08 15:03:57","duration":"0","user":"1548956206177","size":1234124,"type":"video","extension":"mp4","hash":"3423e47523e07d4a62731f6506c1f64b","permissions":""},{"id":"1551972401965","name":"Eric","original_name":"Eric","file":"https:\/\/www.youtube.com\/watch? in C:\xampp\htdocs\Video1D3A\solicitudesok.php on line 90

Pero cuando hago un print_r($resp) sin hacer un file_get_content y solo con el json_decode me lanza todo el JSON con la estructura de la imagen de arriba, en donde quiero acceder a "src" y guardarlo en variable.

Array ( [status] => success [error] => 0 [data] => Array ( [media] => Array ( [0] => Array ( [id] => 1552057437978 [name] => videoCorto.mp4 [original_name] => videoCorto.mp4 [file] => 3423e47523e07d4a62731f6506c1f64b.mp4 [src] => http://mediaexample.net/ec/3423e47523e07d4a62731f6506c1f64b.mp4 [origin] => nfs [uploaded] => 2019-03-08 15:03:57 [modified] => 2019-03-08 15:03:57 [duration] => 0 [user] => 1548956206177 [size] => 1234124 [type] => video [extension] => mp4 [hash] => 3423e47523e07d4a62731f6506c1f64b [permissions] => ) [1] => Array ( [id] => 1551972401965 [name] => Eric [original_name] => Eric [file] => https://www.youtube.com/watch?v=i5fxiSNpO84 [src] => https://www.youtube.com/watch?v=i5fxiSNpO84 [origin] => youtube [uploaded] => 2019-03-07 15:26:41 [modified] => 2019-03-07 15:26:41 [duration] => 0 [user] => 1548956206177 [size] => -1 [type] => video [extension] => unknown [hash] => 1e760ac262891c619899e857948c9521 [permissions] => ) [2] => Array ( [id] => 1548958887843 [name] => 5-0 [original_name] => 5-0 [file] => https://www.youtube.com/watch?v=_oAgGH5wbMA [src] => https://www.youtube.com/watch?v=_oAgGH5wbMA [origin] => youtube [uploaded] => 2019-01-31 18:21:27 [modified] => 2019-01-31 18:21:27 [duration] => 0 [user] => 1548956206177 [size] => -1 [type] => video [extension] => unknown [hash] => 5039a2346f2f63e9754567b0d926552c [permissions] => ) ) ) )

Para luego ese source enviarlo a reproducir en un Video JS.

EDIT 2

Hice este foreach con tal de ir accediendo al src:

$resp = $client->call("ListMedia", array("json_access" => json_encode($access), "json_media_filters"=>json_encode($filters))); $decoded =json_decode($resp,true); foreach($decoded["data"] as $clave => $valor) { print "$clave => $valor\n"; }

Y me lanza lo siguiente:

Notice: Array to string conversion in C:\xampp\htdocs\Video1D3A\solicitudesok.php on line 94

media => Array

Un compañero ha escrito una formula pero no logro entenderlo bien aunque sé que se refiere a acceder al objecto, al array -> "campo que se quiera acceder". Pero como lo implementaria?

Gracias

Renzo
  • 35
  • 10