我目前正在尝试转换json数据,我必须显示为html文本在我的网页,但当它出现它是在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, " ");
targetNode.innerHTML = data;
},
error: function(error){
targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}
// Call the asynchronous xhrGet
var deferred = dojo.xhrGet(xhrArgs);
});
});
现在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.}]
我是否可以过滤或指定“段落”中的数据,以便在html中显示?
任何建议或帮助都将是伟大的!
1条答案
按热度按时间iqjalb3h1#
您已将handleAs属性指定为“text”,这意味着您的响应将仅为纯文本。这将使您难以从响应中解析出所需的特定信息。请将handleAs更改为json,以便您从服务器检索的信息随后转换为javascript对象。
此时,您可以调用data ['Paragraph']或data. Paragraph。如果您想获取其他键/值对的值,这同样适用。如果您想获取Relevance,只需调用
编辑:另外,你的json看起来格式不正确,如果你有handleAs:'json',这会导致错误。