XML to Json code issue

时间:2012-10-26 作者:PHPLearner

我编写了一个脚本,以XML格式显示按类别组织的所有WordPress帖子。代码为

// get all the categories from the database
$cats = get_categories();
header(\'Content-type: text/xml\');
echo \'<categories>\';

// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
echo \'<category>\';
?>

    <cat_id><?php echo $cat_id; ?></cat_id>
    <cat_name><?php echo $cat->name; ?></cat_name>
    <posts>
        <?php
        query_posts("cat=$cat_id&post_per_page=2");
        if (have_posts()) : while (have_posts()) : the_post();
        ?>
<post>
                <name><?php the_title(); ?></name>
                <url><?php echo get_post_meta($post->ID, \'apls_video_url\', true); ?></url>
        </post>
        <?php
        endwhile;
        endif;
        ?>

    </posts>

</category>
<?php } 
echo \'</categories>\';
我还需要JSON格式的输出。我试过了json_encode 输出为

{"categories":{"cat_id":["555","15","14","554","13","23","26","25","27","33","17","30","29","18","28","20","9","22"],"cat_name":["Articles","Crank","Escapes","Grappling Videos","Guard Breaks","Half Guard","Japanese Jujutsu","Leg Locks","MMA","MMA Videos","Mount","North South Position","Self Defense","Side Control","Strikes","Theory & Discussions","Throws","Wrist Locks"],"posts":[{"post":[{"name":"Jiu Jitsu Instruction \\u2013 Time To Teach","url":[]},{"name":"How to deal with the Bystander Effect: Taming a Hostile Crowd","url":[]},{"name":.......
但我需要以下格式的输出

{"categories":[
{ "cat_id": "16",
 "cat_name": "Arm Lock",
   "posts": [
     {"name": "title1", "url": "www.title1.com"},
     {"name": "title2", "url": "www.title2.com"},
     {"name": "title3", "url": "www.title3.com"}
   ]
}

{ "cat_id": "12",
 "cat_name": "Punch face",
   "posts": [
     {"name": "title11", "url": "www.title11.com"},
     {"name": "title12", "url": "www.title12.com"},
     {"name": "title13", "url": "www.title13.com"}
   ]
}
}}
如何将输出固定到所需的格式?

1 个回复
SO网友:l2aelba

使用此函数窗体http://www.php.net/manual/en/function.json-encode.php#80339

function json_format($json)
{
    $tab = "  ";
    $new_json = "";
    $indent_level = 0;
    $in_string = false;

    $json_obj = json_decode($json);

    if($json_obj === false)
        return false;

    $json = json_encode($json_obj);
    $len = strlen($json);

    for($c = 0; $c < $len; $c++)
    {
        $char = $json[$c];
        switch($char)
        {
            case \'{\':
            case \'[\':
                if(!$in_string)
                {
                    $new_json .= $char . "\\n" . str_repeat($tab, $indent_level+1);
                    $indent_level++;
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case \'}\':
            case \']\':
                if(!$in_string)
                {
                    $indent_level--;
                    $new_json .= "\\n" . str_repeat($tab, $indent_level) . $char;
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case \',\':
                if(!$in_string)
                {
                    $new_json .= ",\\n" . str_repeat($tab, $indent_level);
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case \':\':
                if(!$in_string)
                {
                    $new_json .= ": ";
                }
                else
                {
                    $new_json .= $char;
                }
                break;
            case \'"\':
                if($c > 0 && $json[$c-1] != \'\\\\\')
                {
                    $in_string = !$in_string;
                }
            default:
                $new_json .= $char;
                break;                   
        }
    }

    return $new_json;
} 
所以你可以用like

print json_format($your_json);

结束

相关推荐

WordPress正在从post_meta中的JSON字符串中去除转义反斜杠

我认为通过将一些内容保存为自定义post\\u元字段中的JSON片段,可以让我的生活变得轻松,并关注未来。不幸的是,WordPress不同意,这让我的生活变得异常艰难。我有一个JSON字符串,本质上看起来像这样。这只是一位,注释字符串只是一些伪unicode实体。整件事都是通过/json_encode.{ \"0\": { \"name\": \"Chris\", \"url\": \"testdomain.com\",