2013-02-25 87 views
0

如何在php中回顯json?服務器向我發送json。如何在php中正確寫入? json

$i=$_POST["id"]; 
if($i==1){ 

// here i must echo this 

{ 

"item": 

{ 
     "html": 
[{ 

"description": "some text", 
      "n": "1" 
     }], 
     "table": { 

      "1": { 

       "line": [{ 

        "number": "", 

        "value": "" 

       } 

       ] 
      } 

     }, 
     "videos": [], 

     "urlext": [], 

     "imgs": [ { 

      "size": { 

       "root": "xxx xxx" 

      } 

     }] 

    } 

} 


} 
else { 

// here i must echo another one 

{ 

"item": 

{ 
     "html": 
[{ 

"description": "some text", 
      "n": "1" 
     }], 
     "table": { 

      "1": { 

       "line": [{ 

        "number": "", 

        "value": "" 

       } 

       ] 
      } 

     }, 
     "videos": [], 

     "urlext": [], 

     "imgs": [ { 

      "size": { 

       "root": "xxx xxx" 

      } 

     }] 

    } 

} 


} 


} 
+0

回聲json_encode($ yourContent); – sanj 2013-02-25 14:21:53

+2

[json_encode()](http://php.net/manual/en/function.json-encode.php)。你是怎麼找不到Google的,甚至是搜索這個網站的? – SDC 2013-02-25 14:21:56

+0

SO正在被轟炸,顯示沒有研究的努力的問題,這些天..至少PHP的問題 – Broncha 2013-02-25 14:25:19

回答

1

只是生成數據的關聯數組,你需要發送的JSON和使用json_encode()渲染JSON

$data = array('name' => 'foo', 
       'some' => 'thing' 
); 

echo json_encode($data);