if(UserDefaults.standard.string(forKey: "UID") != nil){
guard let dataFile = Bundle.main.path(forResource: "database7", ofType: "json"),
let database = FileManager.default.contents(atPath: dataFile) else {
fatalError("Data not found")
}
let json = try? JSONSerialization.jsonObject(with: database, options: [])
if let json = json as? [String: Any] {
if let jsonChild = json["tired"] as? [[String: Any]] {
for botanical in jsonChild {
var botanicalObject = BotanicModel(json: botanical)
botanicDetail.append(botanicalObject)
print(botanicDetail)
}
}
} else{
fatalError("Failed to parse json")
}
}
所以我試圖決議我的本地資料,但它拋出了錯誤,我無法弄清楚;我無法檢索任何資料,而且資料有點過于復雜,無法自行弄清楚。
下面是模型
struct BotanicModel {
var id: ID
init(id: ID ){
self.id = id
}
init(json: [String:Any]){
self.id = ID(json: json["id"] as? [String:Any] ?? [:])
}
}
struct ID {
var name: String
var therapeutic: [String]
var clinical: [String]
var contraindications: [String]
var drugNutrient: [String]
var chemical: [String]
var toxicity: [String]
init(name: String, therapeutic: [String], clinical: [String], contraindications: [String], drugNutrient: [String], chemical: [String], toxicity: [String]) {
self.name = name
self.therapeutic = therapeutic
self.clinical = clinical
self.contraindications = contraindications
self.drugNutrient = drugNutrient
self.chemical = chemical
self.toxicity = toxicity
}
init(json: [String: Any]) {
self.name = json["name"] as? String ?? "name"
self.therapeutic = json["therapeutic"] as? [String] ?? []
self.clinical = json["clinical"] as? [String] ?? []
self.contraindications = json["contraindications"] as? [String] ?? []
self.drugNutrient = json["drugNutrient"] as? [String] ?? []
self.chemical = json["chemical"] as? [String] ?? []
self.toxicity = json["toxicity"] as? [String] ?? []
}
}
資料如下
{
"tired": [
{
"id":[
{
"name":"Achillea millefolium (Yarrow)",
"therapeutic":[
"? Anodyne - due to prostaglandin-inhibiting action\n? Anti-inflammatory\n? Antiseptic\n? Antispasmodic\n? Astringent\n? Bitter tonic\n? Carminative\n? Cholagogue\n? Decongestant\n? Diaphoretic\nHot infusion - stimulating diaphoretic effect\nCold infusion - diuretic effect or tones gastric organs\n? Diuretic\n? Hemostatic\n? Hypotensive\n? Stimulant\n? Urinary antiseptic"
],
"clinical":[
"? Allergies\n? Dysmenorrhea\n? Hemorrhoids\n? Peptic ulcer\n? Antibacterial:\nGram positive bacteria\nGram negative bacteria\n? Circulatory disorders\n? Hemorrhaging disorders\n? Influenza and colds\n? Lacerations and puncture wounds - topically\n? Menorrhagia with uterine atony\n? Pain associated with pelvic disorders\n? Uterine spasms\n? Vaginitis with vaginal atony"
],
"contraindications":[
"? External use:\nContact dermatitis in sensitive individuals\n? Gastrointestinal inflammation:\nCrohn's disease\nIrritable bowel syndrome\nUlcerative colitis\n? Increased central nervous system function (CNS hyperfunction)\n? Pregnancy:\nDue to the emmenagogue and abortifacient effects"
],
"drugNutrient":[
"? Counterproductive to use medications that inhibit stomach acid production, ie antacids, gastric acid secretion inhibitors and histamine H2 receptor antagonists, since yarrow promotes stomach acid secretion"
],
"chemical":[
"? Achilleic acid (identical to aconitic acid)\n? Alkanes\n? Alkaloids:\nAchilleine\nBetonicine\nStachydrine\n? Apigenin, an antispasmodic agent\n? B-iso-thujone, see Toxicity\n? Betaine\n? Earthly ash consisting of nitrates, phosphates, and chlorides of potash and line\n? Fatty acids:\nLinoleic\nOleic\nPalmatic\n? Lactones\n? Potassium and calcium salts\n? Rutin\n? Salicylic acid (anti-inflammatory anodyne organic acid\n? Saponins\n? Sterols - Beta sitosterol\n? Succinic acid\n? Trigonelline\n? Volatile oils:\nAzulene\nCamphor\nCineol\nSabinene\nPinene"
],
"toxicity":[
"? B-iso-thujone can cause:\nVomiting\nStomach and intestinal cramps\nRetention of urine\n? Extreme cases with large doses:\nConvulsions\nRenal damage\nTremors\nVertigo"
]
}
]
}
]
}
如果您有任何解決此問題的想法,請分享。
我不知道該輸入什么。我已經提到了整個問題,但是 StackOverflow 想讓我詳細解釋一下,所以我寫了這句話。
uj5u.com熱心網友回復:
您的模型結構非常僵化,因為頂部物件被命名為“tired”,這迫使一個變數被這樣命名。如果您正在尋找不同型別的癥狀,您的 JSON 實際上應該是不同的 - 例如,"symptom : tired,"在頂部,然后是"herbs : [ { id:...".
您的 JSON 可以使用以下物件進行解碼:
struct Tired: Codable {
let tired: [HerbCollection]
}
struct HerbCollection: Codable {
let id: [Herb]
}
struct Herb: Codable {
let name: String
let therapeutic: [String]
let clinical: [String]
let contraindications: [String]
let drugNutrient: [String]
let chemical: [String]
let toxicity: [String]
}
以下是解碼方法:
do {
let tired = try JSONDecoder().decode(Tired.self, from: Data(json.utf8))
print(tired.tired[0].id[0].name)
print(tired.tired[0].id[0].therapeutic[0])
print(tired.tired[0].id[0].clinical[0])
// etc.
} catch {
print(error)
}
uj5u.com熱心網友回復:
使用 JSONDecode 而不是 JSONSerialization,然后使用 json 鍵明智地創建模型,然后它將成功執行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512856.html
標籤:json迅速解析嵌套的 json
