这是我的Json数据。
{
"products":[
{
"capacity":4,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-uberx.png",
"display_name":"uberX",
"product_id":"a1111c8c-c720-46c3-8534-2fcdd730040d",
"description":"The low-cost Uber"
},
{
"capacity":6,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-uberxl2.png",
"display_name":"uberXL",
"product_id":"821415d8-3bd5-4e27-9604-194e4359a449",
"description":"low-cost rides for large groups"
},
{
"capacity":4,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-black.png",
"display_name":"UberBLACK",
"product_id":"d4abaae7-f4d6-4152-91cc-77523e8165a4",
"description":"The original Uber"
},
{
"capacity":6,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-suv.png",
"display_name":"UberSUV",
"product_id":"8920cb5e-51a4-4fa4-acdf-dd86c5e18ae0",
"description":"Room for everyone"
},
{
"capacity":4,
"image":"http:\/\/d1a3f4spazzrp4.cloudfront.net\/car-types\/mono\/mono-taxi.png",
"display_name":"uberTAXI",
"product_id":"3ab64887-4842-4c8e-9780-ccecd3a0391d",
"description":"Taxi without the hassle"
}
]
}
我希望所有由键表示的值i.e capacity, image, display_name,product_id, description
并保存在以下给定的列表中
var array_capacity = Array<Int>();
var array_image = Array<String>();
var array_display_name = Array<String>();
var array_product_id = Array<String>();
var array_description = Array<String>();
但它崩溃了。我认为这是由于某些强制转换问题造成的。
这是我的解析代码。
var responseDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary
if(!(responseDict==nil)){
var products: NSArray = responseDict["products"] as NSArray
for item in products { // loop through data items
let obj = item as NSDictionary
for (key, value) in obj {
// print(key as String);
array_product_id.append(value as String);
array_display_name.append(value as String);
array_image.append(value as String);
array_description.append(value as String);
array_capacity.append(value as Int);
}
}
}
日志
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x109c55f30: pushq %rbp
0x109c55f31: movq %rsp, %rbp
0x109c55f34: pushq %rbx
0x109c55f35: pushq %rax
0x109c55f36: movq %rsi, %rcx
0x109c55f39: movq %rdi, %rbx
0x109c55f3c: xorl %eax, %eax
0x109c55f3e: testq %rbx, %rbx
0x109c55f41: je 0x109c55f5d ; swift_dynamicCastObjCClassUnconditional + 45
0x109c55f43: movq 0x7858e(%rip), %rsi ; "isKindOfClass:"
0x109c55f4a: movq %rbx, %rdi
0x109c55f4d: movq %rcx, %rdx
0x109c55f50: callq *0x63112(%rip) ; (void *)0x000000010be04000: objc_msgSend
0x109c55f56: testb %al, %al
0x109c55f58: movq %rbx, %rax
0x109c55f5b: je 0x109c55f64 ; swift_dynamicCastObjCClassUnconditional + 52
0x109c55f5d: addq $0x8, %rsp
0x109c55f61: popq %rbx
0x109c55f62: popq %rbp
0x109c55f63: retq
0x109c55f64: leaq 0xecc7(%rip), %rax ; "Swift dynamic cast failed"
0x109c55f6b: movq %rax, 0x7fbe6(%rip) ; gCRAnnotations + 8
0x109c55f72: int3
0x109c55f73: nopw %cs:(%rax,%rax)
这里的问题是您正在快速地遍历NSDictionary。NSDictionary似乎不符合快速序列,因此您使用键值对NSDictionary进行的迭代会崩溃。对NSDictionary使用allKeys方法,并像对Objective C那样定期提取值,就像这样,
if let jsonData = jsonString?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false){
if let jsonObject = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments, error: nil) as? [String: AnyObject]{
if let products: AnyObject = jsonObject["products"]{
for aProduct in products as NSArray{
for aKey in (aProduct as NSDictionary).allKeys{
println(aKey)
}
}
}
}
}
您做错的一件事是将字典中的值附加到,然后使用动态类型转换将其转换为字符串或整数。那不是这样做的方式,并且再次使您的代码崩溃,您可以选择是否使用if let将其强制转换为类型,然后将该值附加到数组中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句