gpt4 book ai didi

ios - 如何在 Swift/iOS 中解析 Json 并获取所需的值?

转载 作者:可可西里 更新时间:2023-11-01 01:43:51 27 4
gpt4 key购买 nike

这是我的 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"
}
]
}

我希望所有值都由键表示,即容量、图像、显示名称、产品ID、描述,并保存在下面给出的列表中

    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)

}

}

}
}

}

您做错的一件事是将字典中的值附加到,并且您正在使用动态转换将其转换为字符串或 Int。这不是正确的做法,它会再次使您的代码崩溃,您可以选择检查它是否可以使用 if let 转换为类型,然后将该值附加到您的数组。

关于ios - 如何在 Swift/iOS 中解析 Json 并获取所需的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25739894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com