Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
json_decode() can be used to parse the JSON –  Pekka 웃 Mar 2 '11 at 17:38
1  
You want to parse it serverside with PHP or your want to create the objects in the DOM using javascript? –  dmcnelis Mar 2 '11 at 17:43
    
sorry Javascript on the client side. –  madphp Mar 2 '11 at 17:50

3 Answers 3

up vote 2 down vote accepted

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>');

}
share|improve this answer
    
Sorry. Javascript on the client side. –  madphp Mar 2 '11 at 17:50
$json = json_decode($inputJson, true);

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

// do somethig
}
share|improve this answer
    
a simple way to do –  Awais Qarni Mar 2 '11 at 20:07

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.