所以当我在嵌套数组中访问json时,我遇到了问题。我之前只有一个数组和我的。$每个函数完美地工作了json设置。但是我在修改它时遇到了麻烦。 JSON:
{
"tcontent": [{
"Name": "Septicaemia",
"url":"<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/septicaemia.jpg);'>",
"Variations": [{
"condition":"Community-acquired",
"organisms":"Staph. aureus",
"antimicrobial":"Flucloxacillin ",
"alternative":"(non anaphylaxis): ",
"comments": "Perform full septic screen."
}, {
"Condition":"Community-acquired if intra- abdominal source suspected",
"Organisms":"Predominantly Gram negatives and anaerobes Enterococci may also feature",
"Antimicrobial":"Co-amoxiclav 1.2g iv tds",
"Comments":"Perform full septic screen"
}, {
"Condition":"Healthcare-associated",
"Organisms":"Varies",
"Antimicrobial":"Piperacillin",
"Alternative":"Seek advice from Consultant Microbiologist",
"Comments":"Always"
}]
}, {
"Name": "Infective Endocarditis (IE) (pending blood culture results)",
"url":"<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/endocarditis.jpg);'>"
}, {
"Name": "Central Nervous System Infections",
"url":"<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/cns.jpg);'>"
}, {
"Name": "Skin and Soft Tissue Infections",
"url": "<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/skin.jpg);'>"
}, {
"Name": "Diabetic patients with foot infections",
"url": "<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/foot.jpg);'>"
}, {
"Name": "Bone and Joint Infections",
"url": "<a>",
"image":"<<div class='grid' style='background-image:url(img/anatomy/bone.jpg);'>"
}, {
"Name": "Intravascular Line Infections",
"url": "<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/intravascular.jpg);'>"
}, {
"Name": "Urinary Tract Infections",
"url": "<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/urinary.jpg);'>"
}, {
"Name": "Respiratory Tract Infections",
"url": "<a>",
"image":"<div class='grid' style='background-image:url(img/anatomy/respiratory.jpg);'>"
}, {
"Name": "Gastrointestinal Infections",
"url": "<a>",
"image":"<div class='grid' style='backgroundimage:url(img/anatomy/gastrointestinal.jpg);'>"
}]
}
这是我的javascript访问它。
$(function (){
var imp = "Json/therapy.json"
$.getJSON(imp, function(data) {
var info = "<br>";
$.each(data.tcontent, function(i, item) {
if(item.Name=='Septicaemia'){
var search = item.Variations;
$.each(item.Variations, function(j, subitem) {
info += subitem.condition + subitem.organisms + subitem.antimicrobial + subitem.alternative + subitem.comments
});
$(info).appendTo(".menu");
//alert(item)
};
});
});
});
我在var搜索上尝试了很多变种,但似乎没有任何效果。我研究了很多类似的问题,并且我已经坚持了很久。任何可以解决这种情况的光都会非常感激!
答案 0 :(得分:3)
无效的2个原因。
首先,javascript区分大小写,您的版本不同。
subitem.condition
失败:
"Condition":"Community-acquired if intra- abdominal source suspected",
"Organisms":"Predominantly Gram negatives and anaerobes Enterococci may also feature",
"Antimicrobial":"Co-amoxiclav 1.2g iv tds",
"Comments":"Perform full septic screen"
所以将“条件”改为“条件”等等。
第二个原因是
将$(info).appendTo(".menu");
更改为$(".menu").append(info)
;
<强>为什么吗
$(".menu").append(info)
只需将字符串粘贴到选定的DOM元素中即可。
但你使用
$(info)...
和jquery现在做各种奇特的事情。
尝试将其用作DOM选择器,或者创建一个新元素。
因为您的信息以<br>
$(info)开头,尝试创建DOM元素并删除所有文本。仅留下<br>
,因为br不能包含内容。
尝试删除初始<br>
,然后您会看到以下错误:
Uncaught Error: Syntax error, unrecognized expression:Community-acquiredStaph. aureusFlucloxacillin...
例如,如果您要输入$("hahaha")
,Jquery会尝试找到标记<hahaha>
,因此当您移除<br>
时,$(info)
正在寻找标记{ {1}}。
但是因为你的字符串会包含奇怪的字符,如“ - ()”。它会失败。因此出现上述错误。
所以你只能添加这样的html:
<Community-acquiredStaph. aureusFlucloxacillin...>
或使用选择器
$("<span>hahah</span>").appendTo($(".menu"));
$("#myDiv").appendTo($(".menu"));
工作时的示例:
$(info).appendTo(".menu");
使用以下json: