如何将json字符串中的HTML标记解析为jquery

时间:2017-03-03 15:23:03

标签: javascript jquery

我从PHP获取json

[{"autoid":"2","package_id":"a78o5jax32","chapter_title":"General Aspects of Surgery","question_text":"Capsule Endoscopy-False statement is","explanation":"<p>Capsuleê Endoscopy is not suitable for patients with strictures.<\/p>","reference":"Bailey and Love 26th Edition P\/205","options":"Sedation Not RequiredxxxxxxPainless ProcedurexxxxxxVisualize Entire small BowelxxxxxxSuitable For Patients With Stricture Bowel","answer":"3"},{"autoid":"3","package_id":"a78o5jax33","chapter_title":"General Aspects of Surgery","question_text":"Physiological changes in laparoscopy include all excep","explanation":"&#8226; Laparoscopyæ causes increased PCWP, Intra cranial Pressure and decreased Functional Residual capacity after expiration is decreased after laparoscopy gas insuffulation.<br \/>&#8226; There will be Hypercarbia and acidosis (pH decrease)<br \/>","reference":"Ref: Schwartz Page 417\/ 10th Edition","options":"Increases PCWPxxxxxxIncreased ICPxxxxxxDecreased FRCxxxxxxIncreased pH","answer":"4"}]

我想在HTML DOM中显示解释字段。我怎样才能做到这一点。我正在尝试使用代码

success:function(data){
            var finalData = JSON.parse(data);
            setTimeout(function(){
              var explainText = finalData[0].explanation;
              console.log($.parseHTML(explainText));
              $("#explanationTest").html(explainText)
            },100)
              console.log(finalData);
          }

但它在HTML文档中显示<p>Capsuleê Endoscopy is not suitable for patients with strictures.</p>

请帮忙

3 个答案:

答案 0 :(得分:0)

可能是因为段落元素的结束标记在您的JSON数据中被转义:<\/p>。尝试修改它以返回</p>等结束标记。

发件人:

"explanation": "<p>Capsuleê Endoscopy is not suitable for patients with strictures.<\/p>"

"explanation": "<p>Capsuleê Endoscopy is not suitable for patients with strictures.</p>"

答案 1 :(得分:0)

嗯,我知道这与你的不同 但似乎工作正常

https://jsfiddle.net/p71movh8/17/

 var finalData = eval(data);
 var explainText = finalData[0].explanation;
 $("#explanationTest").html(explainText);

希望有所帮助

答案 2 :(得分:0)

作为一种选择,您始终可以使用长期遗忘的纯Javascript:

||