如何解析 Json 和获取所需的值 Swift Ios

标签: iphone json ios Swift
发布时间: 2017/3/4 18:34:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这里是我的 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)

请帮助

解决方法 1:

这里的问题是你要遍历在 NSDictionary swift 的方式。NSDictionary 似乎并不符合 swift 序列所以你迭代 NSDictionary 使用键值崩溃。使用 allKeys 方法为 NSDictionary 和定期为你的值将为目的 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)

        }

      }

    }
  }

}

你正在做的一件事错了追加到字典中的值与您使用动态转换来转换为字符串或整数。这不是做的方法,再次崩溃你的代码,可选检查如果它可以铸造类型使用,如果你让,然后将值添加到您的阵列。

赞助商