2012-03-02 35 views
15

Sto cercando di recuperare il contenuto di json sotto usando una rivista api. L'output di JSON è come questo. voglio il sotto json per convertire in array php.Array da JSON a PHP utilizzando file_get_contents

{ 
"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" 
      } 
     ] 
    }, 

] 
} 

Il mio codice è simile a questo.

<?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>"; 
?> 

Il codice sopra riportato restituisce una matrice vuota. :( Come è possibile convertire il JSON sopra per array di oggetti php. Io sono impotente.

Grazie Haan

+2

probabilmente stai diventando un array vuoto perché l'oggetto JSON è valido. Prova a convalidarlo tramite [JSONLint] (http://jsonlint.com/). –

+1

Qual è l'output di print_r ($ data)? –

+0

@Jack & Aldo: risolto il problema. Codice Shivam S.Kara, finalmente funzionato. – hjaffer2001

risposta

18

Il campione JSON che hai fornito non è valido. Verificare che online con questo JSON Validator http://jsonlint.com/ . è necessario rimuovere la virgola in più sulla linea 59.

One hai jSON valido è possibile utilizzare questo codice per convertirlo in un array

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 
           ) 

         ) 

       ) 

     ) 

) 
+0

@Galbraith - Bene, funziona per me: D – hjaffer2001

7

You JSON non è una stringa valida come già detto da P. Galbraith.

ed ecco la soluzione per questo.

<?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>"; 
?> 

Utilizzare questo codice che funzionerà per voi.

+0

So che stai ricevendo i dati dall'API, quindi non è possibile che tu lo convalidi. Ma il mio codice dato funzionerà per te –

+0

ha funzionato perfettamente per me. Grazie mille: D – hjaffer2001

+0

Puoi scegliere la mia risposta come risposta corretta in modo che altre persone possano usarla. –

2

controllare alcune errore di battitura ''

<?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); 
?> 
+0

grazie mille. controllato. : D – hjaffer2001