简体   繁体   English

解析由php json_encode创建的JSON数据

[英]Parse JSON data created by php json_encode

I want to be able to parse the following json data. 我希望能够解析以下json数据。 It was constructed from a php array using jsonencode. 它是使用jsonencode从php数组构造的。 I've added the json below to help you understand it. 我在下面添加了json以帮助您理解它。 I'd like to be able to display the json in a bulleted form. 我希望能够以项目符号形式显示json。 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 EPL

UPDATE: Sorry i need to parse it in javascript. 更新:对不起,我需要用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? 更新您是否在问如何在PHP或Javascript / jQuery中做到这一点? You didn't quite explain what you were doing with it. 您没有完全解释自己在做什么。

UPDATE Here it is using Javascript/jQuery: http://jsfiddle.net/wgjjR/ 更新这里使用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_decode

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

Repeat to extract all the values you require. 重复提取所需的所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM