开发者

Parse JSON data created by php json_encode

I want to be able to parse the following json data. It was constructed from a php array using jsonencode. I've added the json below to help you understand it. I'd like to be able to display the json in a bulleted form. It show two records with associated category array and tags array. Im open to using any libraries to help.

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

I want to be able to parse it and display like this.

  • Liverpool Football Club
  • Content goes here
  • Categories
    • Football Club
  • Tags
    • England
    • EPL

UPDATE: Sorry i need to parse it in javascript.


Try this:

$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 Are you asking on how to do this in PHP or in Javascript/jQuery? You didn't quite explain what you were doing with it.

UPDATE Here it is using 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>');

}


$json = json_decode($inputJson, true);

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

// do somethig
}


Use json_decode

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

Repeat to extract all the values you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜