2014-12-06 67 views
0

我正在使用drupal JSON字段格式化程序对HTML正文字段进行编码。如何将编码的json解码为HTML JS

我得到这个结果JSON

"\"\u2022 Big eggplant\r\n\u2022 2 tomatoes cut in slices\r\n\u2022 1 green pepper cut in slices\r\n\u2022 250gm minced meat\r\n\u2022 \u00bdtbsp of salt\r\n\u2022 \u00bdtbsp dried oregano\r\n\u2022 1tsp black pepper\r\n\u2022 \u00bd cup chopped onions\r\n\u2022 \u00bc cup oil to cook the meat\"" 

使用JS如何解码呢?

我想这个代码,但它不工作的罚款

function htmlbody(x){ 
     var bodi=''; 
bodi=x.replace('\n', '<br />')     // Removes all encoded newline characters 
bodi=x.replace('\t', '')     // Removes all encoded tab characters 
bodi=x.replace(/(?:\s+)?<(?:\s+)?/g, '<') // Removes any whitespace before or after a tag-start delimiter. 
bodi=x.replace(/(?:\s+)?>(?:\s+)?/g, '>') // Removes any whitespace before or after a tag-stop delimiter. 
bodi=x.replace(/\s+/g, ' '); 
return bodi; 
} 
+0

你有没有尝试过这一点?如果你愿意的话,代码会有所帮助。 – rfornal 2014-12-06 01:30:21

+0

@rfornal我只是为你更新我的问题 – lina 2014-12-06 01:32:28

+0

如果你在服务器上预先设置了这个格式,并且让JS接收到格式正确的数据,那最好。 – Joseph 2014-12-06 01:36:19

回答

0

我怀疑的是,你有什么非常接近...

尝试......

function htmlbody(x){ 
    var bodi=''; 
    bodi=x.replace('\n', '<br />')     // Removes all encoded newline characters 
    // From this point, bodi is corrected version; x has not been changed. 
    bodi=body.replace('\t', '')     // Removes all encoded tab characters 
    bodi=bodi.replace(/(?:\s+)?<(?:\s+)?/g, '<') // Removes any whitespace before or after a tag-start delimiter. 
    bodi=bodi.replace(/(?:\s+)?>(?:\s+)?/g, '>') // Removes any whitespace before or after a tag-stop delimiter. 
    bodi=bodi.replace(/\s+/g, ' '); 
    return bodi; 
} 

你也可以看看像这样的东西... json2html.com