[英]How to get to the bottom of a JSON tree
今天,我可能會提出一個新手問題。 我正在嘗試從客戶端服務器解析JSON響應,但由於無法實際訪問其中的數據,因此似乎遇到了麻煩。 這是我要嘗試的JSON內容。 我只需要名稱中的name:"Topic3"
'{
links: [ ],
content: [
{
links: [ ],
content: {
name: "Topic3",
technology: {
name: "Java",
technology_id: 1
},
topic_id: 3
},
id: null
},
],
id: null
}'
任何幫助將不勝感激。 這也是我嘗試使用的JS代碼。
$.ajax({
type:'GET',
dataType: 'json',
url: "technologies/"+ option+ "/topics"
}).then(function(data)
{
for(i=0;i<data.content.length;i++)
{
var topic = document.createElement("button");
var content= (Object.keys(data.content[i]));
topic.name= content.content.name;
AddTopic.appendChild(topic);
}
});
要設置按鈕文本,請使用innerHTML
屬性
var topic = document.createElement("button");
var content = data.content[i];
topic.innerHTML = content.content.name;
AddTopic.appendChild(topic);
因此,我假設您需要遍歷並找到每個可用的主題-在這種情況下,我認為這應該可行
$.ajax({ type:'GET', dataType: 'json', url: "technologies/"+ option+ "/topics" }).then(function(data) { for(i of data.content) { var topic = document.createElement("button"); topic.name= i.content.name; AddTopic.appendChild(topic); } });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.