我希望能够解析以下json数据。它是使用jsonencode从php数组构建的。我在下面添加了json以帮助您理解它。我希望能够以项目符号的形式显示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}}
我希望能够解析它并显示如下。
更新:抱歉,我需要在javascript中解析它。
答案 0 :(得分: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>');
}
答案 1 :(得分:1)
$json = json_decode($inputJson, true);
foreach($json as $key => $value)
{
// do somethig
}
答案 2 :(得分:0)
使用json_decode
$json = json_decode($some_json, true);
$element1 = $json["item"]["element1"];
$element2 = $json["item"]["element2"];
重复以提取所需的所有值。