1

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.

3
  • json_decode() can be used to parse the JSON Commented Mar 2, 2011 at 17:38
  • 1
    You want to parse it serverside with PHP or your want to create the objects in the DOM using javascript? Commented Mar 2, 2011 at 17:43
  • sorry Javascript on the client side. Commented Mar 2, 2011 at 17:50

3 Answers 3

2

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry. Javascript on the client side.
1
$json = json_decode($inputJson, true);

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

// do somethig
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.