read file_get_contents php arrays file-get-contents json

file_get_contents - php read json file



JSON a PHP Array usando file_get_contents (3)

Estoy tratando de obtener el contenido json a continuación utilizando una revista api. La salida del json es así. Quiero que el siguiente json se convierta a php array.

{ "bpath": "http://www.sampledomain.com/", "clist": [ { "cid": "11", "display_type": "grid", "ctitle": "abc", "acount": "71", "alist": [ { "aid": "6865", "adate": "2 Hours ago", "atitle": "test", "adesc": "test desc", "aimg": "", "aurl": "?nid=6865", "weburl": "news.php?nid=6865", "cmtcount": "0" }, { "aid": "6857", "adate": "20 Hours ago", "atitle": "test1", "adesc": "test desc1", "aimg": "", "aurl": "?nid=6857", "weburl": "news.php?nid=6857", "cmtcount": "0" } ] }, { "cid": "1", "display_type": "grid", "ctitle": "test1", "acount": "2354", "alist": [ { "aid": "6851", "adate": "1 Days ago", "atitle": "test123", "adesc": "test123 desc", "aimg": "", "aurl": "?nid=6851", "weburl": "news.php?nid=6851", "cmtcount": "7" }, { "aid": "6847", "adate": "2 Days ago", "atitle": "test12345", "adesc": "test12345 desc", "aimg": "", "aurl": "?nid=6847", "weburl": "news.php?nid=6847", "cmtcount": "7" } ] }, ] }

Mi código se ve así.

<?php $json_url = "http://api.testmagazine.com/test.php?type=menu"; $json = file_get_contents($json_url); $data = json_decode($json, TRUE); echo "<pre>"; print_r($data); echo "</pre>"; ?>

El código anterior devuelve una matriz vacía. :( ¿Cómo es posible convertir el JSON anterior a la matriz de objetos php. Estoy indefenso.

Gracias haan


JSON no es una cadena válida como P. Galbraith te ha dicho anteriormente.

Y aquí está la solución para ello.

<?php $json_url = "http://api.testmagazine.com/test.php?type=menu"; $json = file_get_contents($json_url); $json=str_replace(''}, ]'',"} ]",$json); $data = json_decode($json); echo "<pre>"; print_r($data); echo "</pre>"; ?>

Utilice este código que funcionará para usted.


La muestra JSON que proporcionó no es válida. Compruébelo en línea con este JSON Validator JSONLint . Es necesario eliminar la coma adicional en la línea 59.

Una vez que tenga un json válido, puede usar este código para convertirlo en una matriz.

json_decode ($ json, true);

Array ( [bpath] => http://www.sampledomain.com/ [clist] => Array ( [0] => Array ( [cid] => 11 [display_type] => grid [ctitle] => abc [acount] => 71 [alist] => Array ( [0] => Array ( [aid] => 6865 [adate] => 2 Hours ago [atitle] => test [adesc] => test desc [aimg] => [aurl] => ?nid=6865 [weburl] => news.php?nid=6865 [cmtcount] => 0 ) [1] => Array ( [aid] => 6857 [adate] => 20 Hours ago [atitle] => test1 [adesc] => test desc1 [aimg] => [aurl] => ?nid=6857 [weburl] => news.php?nid=6857 [cmtcount] => 0 ) ) ) [1] => Array ( [cid] => 1 [display_type] => grid [ctitle] => test1 [acount] => 2354 [alist] => Array ( [0] => Array ( [aid] => 6851 [adate] => 1 Days ago [atitle] => test123 [adesc] => test123 desc [aimg] => [aurl] => ?nid=6851 [weburl] => news.php?nid=6851 [cmtcount] => 7 ) [1] => Array ( [aid] => 6847 [adate] => 2 Days ago [atitle] => test12345 [adesc] => test12345 desc [aimg] => [aurl] => ?nid=6847 [weburl] => news.php?nid=6847 [cmtcount] => 7 ) ) ) ) )


Compruebe algunos errores tipográficos '',''

<?php //file_get_content(url); $jsonD = ''{ "bpath":"http://www.sampledomain.com/", "clist":[{ "cid":"11", "display_type":"grid", "ctitle":"abc", "acount":"71", "alist":[{ "aid":"6865", "adate":"2 Hours ago", "atitle":"test", "adesc":"test desc", "aimg":"", "aurl":"?nid=6865", "weburl":"news.php?nid=6865", "cmtcount":"0" }, { "aid":"6857", "adate":"20 Hours ago", "atitle":"test1", "adesc":"test desc1", "aimg":"", "aurl":"?nid=6857", "weburl":"news.php?nid=6857", "cmtcount":"0" } ] }, { "cid":"1", "display_type":"grid", "ctitle":"test1", "acount":"2354", "alist":[{ "aid":"6851", "adate":"1 Days ago", "atitle":"test123", "adesc":"test123 desc", "aimg":"", "aurl":"?nid=6851", "weburl":"news.php?nid=6851", "cmtcount":"7" }, { "aid":"6847", "adate":"2 Days ago", "atitle":"test12345", "adesc":"test12345 desc", "aimg":"", "aurl":"?nid=6847", "weburl":"news.php?nid=6847", "cmtcount":"7" } ] } ] } ''; $parseJ = json_decode($jsonD,true); print_r($parseJ); ?>