2011-03-02 42 views
1

我想能夠解析下面的json數據。它是使用jsonencode從php數組構建的。我已經添加了下面的json來幫助你理解它。我希望能夠以項目符號的形式顯示json。它顯示具有關聯的類別數組和標籤數組的兩條記錄。我打開使用任何庫來幫助。解析由php創建的JSON數據json_encode

{"0":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"908","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Liverpool Football Club","post_content":"Content goes here...","post_name":"liverpoolfc","guid":"http://www.liverpoolfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0},"1":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"907","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Everton Football Club","post_content":"Content goes here","post_name":"evertonfc","guid":"http://www.evertonfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0}} 

我想能夠解析它並以這樣的方式顯示。

  • 利物浦足球俱樂部
  • 內容去 這裏
  • 分類
    • 足球俱樂部
  • 標籤
    • 英格蘭
    • EPL

更新:對不起,我需要解析它在JavaScript中。

+0

'json_decode()'可以用來解析要分析它與PHP或你想服務器端的JSON – 2011-03-02 17:38:07

+1

使用JavaScript創建DOM中的對象? – dmcnelis 2011-03-02 17:43:43

+0

對不起,在客戶端的Javascript。 – madphp 2011-03-02 17:50:24

回答

2

試試這個:

$json = '{"0":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"908","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Liverpool Football Club","post_content":"Content goes here...","post_name":"liverpoolfc","guid":"http://www.liverpoolfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0},"1":{"categories":[{"name":"Football Club","slug":"football-club"}],"tags":[{"name":"England","slug":"england"},{"name":"EPL","slug":"epl"},{"name":"Europe","slug":"europe"},{"name":"Champions","slug":"champions"}],"ID":"907","post_author":"78350","post_date":"2010-10-18 10:49:16","post_title":"Everton Football Club","post_content":"Content goes here","post_name":"evertonfc","guid":"http://www.evertonfc.tv","post_type":"post","comment_count":"0","comment_status":"open","relevance_count":0}}'; 

$array = json_decode($json, true); 

foreach ($array as $item) { 

    echo '<ul>' . PHP_EOL; 
    echo '<li>' . $item['post_title'] . '</li>' . PHP_EOL; 
    echo '<li>' . $item['post_content'] . '</li>' . PHP_EOL; 

    /* Display Categories */ 
    echo '<li>Categories' . PHP_EOL; 
    echo '<ul>' . PHP_EOL; 
    if (!empty($item['categories'])) { 
     foreach ($item['categories'] as $category) { 
      echo '<li>' . $category['name'] . '</li>' . PHP_EOL; 
     } 
    } else { 
     echo '<li>No Categories Available</li>' . PHP_EOL; 
    } 
    echo '</ul>' . PHP_EOL; 
    echo '</li>' . PHP_EOL; 

    /* Display Tags */ 
    echo '<li>Tags' . PHP_EOL; 
    echo '<ul>' . PHP_EOL; 
    if (!empty($item['tags'])) { 
     foreach ($item['tags'] as $tag) { 
      echo '<li>' . $tag['name'] . '</li>' . PHP_EOL; 
     } 
    } else { 
     echo '<li>No Tags Available</li>' . PHP_EOL; 
    } 
    echo '</ul>' . PHP_EOL; 
    echo '</li>' . PHP_EOL; 

    echo '</ul>' . PHP_EOL; 

} 

UPDATE你問如何做到這一點在PHP或在Javascript/jQuery的?你沒有完全解釋你在做什麼。

UPDATE這是使用Javascript/jQuery的:http://jsfiddle.net/wgjjR/

//<div id="container"></div> 

//var json = {}; // this is your JSON object 

var container = $('#container'), html = []; 

for (var key in json) { 

    var item = json[key]; 

    html.push('<ul>'); 
    html.push('<li>' + item.post_title + '</li>'); 
    html.push('<li>' + item.post_content + '</li>'); 

    html.push('<li>Categories<ul>'); 
    for (var cat in item.categories) { 
     cat = item.categories[cat]; 
     html.push('<li>' + cat.name + '</li>'); 
    } 
    html.push('</ul></li>'); 

    html.push('<li>Tags<ul>'); 
    for (var tag in item.tags) { 
     tag = item.tags[tag]; 
     html.push('<li>' + tag.name + '</li>'); 
    } 
    html.push('</ul></li>'); 

    html.push('</ul>'); 

} 
+0

對不起。 Javascript在客戶端。 – madphp 2011-03-02 17:50:44

0

使用json_decode

$json = json_decode($some_json, true); 
$element1 = $json["item"]["element1"]; 
$element2 = $json["item"]["element2"]; 

重複提取所有所需的值。

1
$json = json_decode($inputJson, true); 

foreach($json as $key => $value) 
{ 

// do somethig 
} 
+0

一個簡單的方法來做 – 2011-03-02 20:07:10