JSON中的HTML响应

时间:2012-10-04 05:48:02

标签: html json escaping

我有一个JSON文件,例如

[
   {
      "heading":"Apprenticeship Pathways",
      "image":"http:\/\/www.domain.co.uk\/dfdf\/dfdf/images\/content\/join_our_team\/01.png",
      "columns":[
         {
            "data":"<p>Curabitursapienligulasuscipitneccondimentu mneclaciniaavelitCraslaciniaodiositametvel itpellentesquefringillaPellentesquedapibus0 purusvitaemetusultricesultricesCrasvelorci mneclaciniaavelitCraslaciniaodiositametvel velitinhendreritarcicesultricesCrasvelorci5. velitinhendreritarcicesultricesCrasvelorci5.<\\p>"
         },

输入列数据时出现的问题是“&lt; \ p&gt;”在屏幕上可见,即。逃脱的斜线没有被删除。

function json_request( url, callback ) {
    $.ajax({
        url: url,
        dataType: 'json',
        success: function( data ) {
            callback( data );
        },
        error : function(){
            callback( false );
        }
    });
}

function build_html( $obj ) {
    var $firstCol = $obj.columns.shift();
    var $columns = '';
    for( var i in  $obj.columns ) {
        $columns += '<div class="hori-con">';
        $columns += $obj.columns[i].data;
        $columns += '</div>';
    }
    var $return = 
        '   <li>' + 
        '       <div class="gridRow cf">' + 
        '           <div class="twelveCol last">' +
        '               <div class="cf hori-con-inner">' +
        '                   <div class="hori-con">' +
        '                       <img src="' + $obj.image + '">' +
        '                       <h3>' + $obj.heading + '</h3>' + 
                                $firstCol.data +
        '                       <a href="#" class="hori-open">open</a>' +
        '                   </div>' +
                            $columns +
        '               <div>' +
        '           </div>' +
        '       </div>' +
        '   </li>';
    return $return;
}

function filter_jobs(){
    $('#join_our_team .sub-heading-bar a.nav-box').bind($bind , function(){
        var $this = $(this),
            $qsKey = 'filter',
            $href = $this[0].href,
            $basename = $href.split('?' + $qsKey + '=')[1],
            $dataContainer = $('#joinOurTeamScroller ul.thelist');

        if( typeof $basename !== 'undefined' ) {
            json_request('./json/' + $basename + '.json', function( $data ){
                $html = '';
                if( $data !== false ) {
                    $.each( $data, function(key, val) {
                        $html += build_html( val );       
                    }); 
                }
                $dataContainer.html($html);
            });
        }
        return false;                                    
    });
}

是否有标准化的方法来执行此操作,还是需要搜索并替换斜杠?

0 个答案:

没有答案