Javascript Json显示为HTML文本-非原始格式

Javascript Json显示为HTML文本-非原始格式,javascript,json,dojo,Javascript,Json,Dojo,我目前正在尝试转换我必须在页面中显示为html文本的json数据,但是当它出现时,它是json格式的。因此,我想知道是否有可能消除所有括号等,并有平面html段落 require(["dojo"], function (dojo){ dojo.ready(function(){ // Look up the node we'll stick the text under. var targetNode = dojo.byId("licenseContainer"); // The pa

我目前正在尝试转换我必须在页面中显示为html文本的json数据,但是当它出现时,它是json格式的。因此,我想知道是否有可能消除所有括号等,并有平面html段落

require(["dojo"], function (dojo){

dojo.ready(function(){
// Look up the node we'll stick the text under.
var targetNode = dojo.byId("licenseContainer");  


// The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
var xhrArgs = {
url: "",
handleAs: "text",
timeout : 2000,

load: function(data){
  // Replace newlines with nice HTML tags.
  data = data.replace(/\n/g, "<br>");

  // Replace tabs with spaces.
  data = data.replace(/\t/g, "&nbsp;&nbsp;&nbsp;");

  targetNode.innerHTML = data;
},
error: function(error){
  targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}

 // Call the asynchronous xhrGet
 var deferred = dojo.xhrGet(xhrArgs);
});

});
我是否能够筛选或指定“段落”中的数据,以便在html中显示


任何建议或帮助都会很好

您已将handleAs属性指定为“text”,这意味着您的响应将是纯文本。这将很难从响应中解析出您想要的特定信息。将handleAs更改为json,以便从服务器检索的信息随后转换为javascript对象

handleAs:'json'
此时,您可以调用data['paragration']或data.paragration。如果您想获取其他键/值对的值,同样的方法也适用。如果你想要相关性,你只需要打电话

alert(data.Relevance);
编辑: 另一件事,您的json看起来格式不正确,如果您有handleAs:“json”,这将导致错误。应该是这样的

{"Relevance":"Low","Name":"Clinton","id":1,"Paragraph":"Appointed secretary of state at the start of Mr Obama's first term, in January 2009, Mrs Clinton's health has been under intense scrutiny because she is considered a strong candidate for the Democratic nomination for president should she decide to run in 2016."}

使用数据[“段落”]。。喜欢谢谢你的回复,但我不明白你想说什么如果你想显示json中的段落,那么你可以指定数据[“段落”]。。像这样……哦,对了。但是,当我实现'data=data.[“段落”];'我的json不再显示。有什么想法吗?嗨,我一直想把handleAs改为:“json”,但我一直遇到一个错误,当我把“json而不是text”改为“json”时,出现了一个意外错误:TypeError:“undefined”不是一个函数“有什么想法吗?为什么?所以在它进行异步调用之后,它会调用您的错误处理程序,对吗?如果是这种情况,您的json可能格式不正确。根据您的json示例判断,段落中的数据应该是引号。应该是“段落”:“在2009年1月奥巴马第一个任期开始时任命国务卿,希拉里的健康状况一直受到密切关注,因为如果她决定在2016年参选,她被认为是民主党总统提名的有力候选人。”你还连续有两个逗号。它应该是这样的:这是一个随机url示例,这就是为什么您可能无法访问它:)。是的,它调用了我的错误处理程序,我已经测试并查看了我的json-没有错误。所以这不是我的json。。是不是我需要别的东西?或者我的脚本标签需要声明一个类型吗?是的,我的json数据格式正确,我通过它来确保。我仍然有相同的错误。请检查此jsbin链接。第一个警告是关于JSON的更改,我建议删除额外的逗号,在段落值周围添加引号,并删除括号。第二部分是您提供的JSON。当我尝试使用dojo JSON解析器将其从字符串解析为JSON时,您会注意到它得到一个未定义的错误。
{"Relevance":"Low","Name":"Clinton","id":1,"Paragraph":"Appointed secretary of state at the start of Mr Obama's first term, in January 2009, Mrs Clinton's health has been under intense scrutiny because she is considered a strong candidate for the Democratic nomination for president should she decide to run in 2016."}