Also ich versuche, diese eingebetteten Werte aus dieser JSON-Datei mit Swiftyjson zu analysieren, aber ich kann nicht herausfinden, wie man die eingebetteten Sachen aus dem JSON bekommen.parsen eingebettete Json mit Swiftyjson und Swift
Dies ist, was ich bisher habe und es funktioniert für die oberen Ebenen von Json heraus, aber nicht für die verschachtelten Elemente. Die Hauptsache, die ich aus dieser Datei brauche, ist der Abschnitt "Items" für den Namen und die erstellten Werte in den Elementen.
if let data = json.dataUsingEncoding(NSUTF8StringEncoding) {
let newJson = JSON(data: data)
myBarcode = newJson["barcode_id"].stringValue
myName = newJson["name"].stringValue
myTotalPointsEarned = newJson["total_points_earned"].stringValue
myPointsEarned = newJson["points_available_to_spend"].stringValue
myRank = newJson["rank"].stringValue
myId = newJson["id"].stringValue
//new json arrary to get the items and dates
var myHistory = newJson["order_history"].arrayValue
print("\n My Hist \n" , myHistory)
//FAIL
//var myItems = newJson["items"].stringValue
//print("\n My Items \n" , myItems)
}
Und das ist die JSON-Datei, die ich
{
"id" : "xxx",
"name" : "xfgsfsdfs",
"total_points_earned" : null,
"points_available_to_spend" : null,
"rank" : null,
"barcode_id" : "C-00000252",
"order_history" : [ {
"items" : [ {
"id" : 284,
"created" : [ 2016, 5, 26, 5, 27, 53 ],
"updated" : [ 2016, 5, 26, 5, 27, 53 ],
"sku" : "10-10-08-050",
"name" : "Halloween stuff",
"description" : "",
"quantity" : 1.0,
"price" : 2000.0,
"total" : 2000.0,
"tax" : null,
"discount" : null
}, {
"id" : 285,
"created" : [ 2016, 5, 26, 5, 27, 53 ],
"updated" : [ 2016, 5, 26, 5, 27, 53 ],
"sku" : "10-22-12-247",
"name" : "More Xmas stuff",
"description" : "",
"quantity" : 1.0,
"price" : 2300.0,
"total" : 2300.0,
"tax" : null,
"discount" : null
}, {
"id" : 286,
"created" : [ 2016, 5, 26, 5, 27, 53 ],
"updated" : [ 2016, 5, 26, 5, 27, 53 ],
"sku" : "10-22-12-249",
"name" : "Xmas stuff",
"description" : "",
"quantity" : 1.0,
"price" : 3700.0,
"total" : 3700.0,
"tax" : null,
"discount" : null
} ],
"items" : [ {
"id" : 288,
"created" : [ 2016, 5, 26, 5, 29, 51 ],
"updated" : [ 2016, 5, 26, 5, 29, 51 ],
"sku" : "JJ-02-00-042",
"name" : "A sample product name",
"description" : "",
"quantity" : 1.0,
"price" : 3000.0,
"total" : 3000.0,
"tax" : null,
"discount" : null
} ]
}
]
}
Vielen Dank für jede Hilfe bei diesem
ich tat kleiner Fehler in meiner Antwort :) Wie Eric betont, sollte ich die Daten nicht auspacken, da du swifty json verwendest :) Bitte sieh dir die aktualisierte Antwort an, falls du irgendein Problem hast :) –