[英]Complex JSON nesting of objects and arrays
我對JSON對象/數組的語法和結構很困難。
{
"accounting" : [
{ "firstName" : "John",
"lastName" : "Doe",
"age" : 23 },
{ "firstName" : "Mary",
"lastName" : "Smith",
"age" : 32 }
],
"sales" : [
{ "firstName" : "Sally",
"lastName" : "Green",
"age" : 27 },
{ "firstName" : "Jim",
"lastName" : "Galley",
"age" : 41 }
]
}
我想創建一個嵌套的對象和數組結構,它將包含以下信息:
{
"problems": [{
"Diabetes":[{
"medications":[{
"medicationsClasses":[{
"className":[{
"associatedDrug":[{
"name":"asprin",
"dose":"",
"strength":"500 mg"
}],
"associatedDrug#2":[{
"name":"somethingElse",
"dose":"",
"strength":"500 mg"
}]
}],
"className2":[{
"associatedDrug":[{
"name":"asprin",
"dose":"",
"strength":"500 mg"
}],
"associatedDrug#2":[{
"name":"somethingElse",
"dose":"",
"strength":"500 mg"
}]
}]
}]
}],
"labs":[{
"missing_field": "missing_value"
}]
}],
"Asthma":[{}]
}]}
但我不知道這樣做的正確方法是什么。 我應該只是制作JavaScript對象嗎? JSON對這個項目有意義嗎?
設置這樣的東西的正確語法是什么?
到目前為止,這是我的代碼:
$(document).ready(function() {
$.getJSON('js/orders.json', function(json) {
$.each(json.problems, function(index, order) {
$('.loadMeds').append('<p>' + order.name + '</p>')
});
});
});
第一個代碼是Javascript代碼的示例,它類似,但不是JSON。 JSON不會有1)注釋和2) var
關鍵字
你的JSON中沒有任何注釋,但你應該刪除var
並像這樣開始:
orders: {
[{}]
符號表示“數組中的對象”,並不是您需要的所有內容。 這不是錯誤,但對於某些目的而言太復雜了。 AssociatedDrug應該作為一個對象很好地工作:
"associatedDrug": {
"name":"asprin",
"dose":"",
"strength":"500 mg"
}
此外,空對象實驗室應該填充一些東西。
除此之外,你的代碼還可以。 您可以將其粘貼到javascript中,也可以使用JSON.parse()
方法或任何其他解析方法(請不要使用eval )
更新2回答:
obj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name
返回'阿司匹林'。 然而,它更適合各地的森林
我成功解決了我的問題。 這是我的代碼:
復雜的JSON對象:
{
"medications":[{
"aceInhibitors":[{
"name":"lisinopril",
"strength":"10 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"antianginal":[{
"name":"nitroglycerin",
"strength":"0.4 mg Sublingual Tab",
"dose":"1 tab",
"route":"SL",
"sig":"q15min PRN",
"pillCount":"#30",
"refills":"Refill 1"
}],
"anticoagulants":[{
"name":"warfarin sodium",
"strength":"3 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"betaBlocker":[{
"name":"metoprolol tartrate",
"strength":"25 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"diuretic":[{
"name":"furosemide",
"strength":"40 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"mineral":[{
"name":"potassium chloride ER",
"strength":"10 mEq Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}]
}
],
"labs":[{
"name":"Arterial Blood Gas",
"time":"Today",
"location":"Main Hospital Lab"
},
{
"name":"BMP",
"time":"Today",
"location":"Primary Care Clinic"
},
{
"name":"BNP",
"time":"3 Weeks",
"location":"Primary Care Clinic"
},
{
"name":"BUN",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Cardiac Enzymes",
"time":"Today",
"location":"Primary Care Clinic"
},
{
"name":"CBC",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Creatinine",
"time":"1 Year",
"location":"Main Hospital Lab"
},
{
"name":"Electrolyte Panel",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Glucose",
"time":"1 Year",
"location":"Main Hospital Lab"
},
{
"name":"PT/INR",
"time":"3 Weeks",
"location":"Primary Care Clinic"
},
{
"name":"PTT",
"time":"3 Weeks",
"location":"Coumadin Clinic"
},
{
"name":"TSH",
"time":"1 Year",
"location":"Primary Care Clinic"
}
],
"imaging":[{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
},
{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
},
{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
}
]
}
用於獲取數據並在我的網頁上顯示的jQuery代碼:
$(document).ready(function() {
var items = [];
$.getJSON('labOrders.json', function(json) {
$.each(json.medications, function(index, orders) {
$.each(this, function() {
$.each(this, function() {
items.push('<div class="row">'+this.name+"\t"+this.strength+"\t"+this.dose+"\t"+this.route+"\t"+this.sig+"\t"+this.pillCount+"\t"+this.refills+'</div>'+"\n");
});
});
});
$('<div>', {
"class":'loaded',
html:items.join('')
}).appendTo("body");
});
});
確保遵循JSON的語言定義。 在您的第二個示例中,部分:
"labs":[{
""
}]
是無效的,因為對象必須由零個或多個鍵-值對"a" : "b"
,其中"b"
可以是任何有效的值 。 一些解析器可能會自動將{ "" }
解釋為{ "" : null }
,但這不是一個明確定義的情況。
此外,您正在使用嵌套的對象數組 [{}]
。 我只會這樣做:
首先,選擇數據結構(xml,json,yaml)通常只包括可讀性/大小問題。 例如
Json非常緊湊,但沒有人可以輕松閱讀,很難調試,
Xml非常大,但每個人都可以輕松地讀取/調試它,
Yaml位於Xml和json之間。
但是如果你想大量使用Javascript和/或你的軟件在瀏覽器 - 服務器之間進行大量的數據傳輸,你應該使用Json,因為它是純粹的javascript並且非常緊湊。 但是不要嘗試在字符串中編寫它,使用庫來生成對象所需的代碼。
希望這可以幫助。
您可以嘗試使用此函數來查找嵌套的嵌套數組中的任何對象。
例
function findTByKeyValue (element, target){
var found = true;
for(var key in target) {
if (!element.hasOwnProperty(key) || element[key] !== target[key]) {
found = false;
break;
}
}
if(found) {
return element;
}
if(typeof(element) !== "object") {
return false;
}
for(var index in element) {
var result = findTByKeyValue(element[index],target);
if(result) {
return result;
}
}
};
findTByKeyValue(problems,{"name":"somethingElse","strength":"500 mg"}) =====> result equal to object associatedDrug#2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.