如何将编码的json解码为HTML JS

时间:2014-12-06 01:29:20

标签: javascript jquery drupal

我正在使用drupal JSON字段格式化程序来编码HTML正文字段。

我在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;
}

1 个答案:

答案 0 :(得分: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