【问题标题】:swift vapor json response to array对数组的快速蒸汽json响应
【发布时间】:2017-05-03 08:56:08
【问题描述】:

使用 swift vapor 和 elasticsearch,得到如下响应:

{ "_shards": { "failed": 0, "successful": 5, "total": 5 }, "hits": { "hits": [ { "_id": "3", "_index": "items_v1", "_score": 1.2029922, "_source": { "property1": "test", "property2": "another test", ... }, "_type": "item" }, ...

在“hits”->“hits”->“_source”中,我得到了模型“Item”的所有属性。如何从这个 json 响应中创建一个 Items 数组“[Item]”?

【问题讨论】:

    标签: arrays swift elasticsearch swift3 vapor


    【解决方案1】:

    小增强,使用guard语句避免嵌套ifs...

    guard
      let dict = response as? [String : Any],
      let hits = dict["hits"] as? [String : Any],
      let hitArray = hits["hits"] as? [[String : Any]]
    else
    { throw Abort}
    
    for hit in hitArray {
       if let source = hit["_source"] {
            arrayOfItems.append(Item(with: source))
       }
    }
    

    【讨论】:

      【解决方案2】:

      以这种方式解析您的响应,因此如果某些值不会被发送,则不会发生崩溃。

      if let dict = response as? [String : Any] {
          if let hits = dict["hits"] as? [String : Any] {
              if let hitArray = hits["hits"] as? [[String : Any]] {
                  for hit in hitArray {
                      if let source = hit["_source"] {
                          arrayOfItems.append(Item(with: source))
                      }
                  }
              }
          }
      }
      

      在您的 Item 类中创建 init 方法,您将在其中初始化 item 的属性。

      init(with dict: [String : Any]) {
      
          if let property1 = dict["property1"] as? Int {
              self.property1 = property1
          }
      
          super.init()
      }
      

      【讨论】:

      • 好的,可以使用这种方法,认为会有更优雅的解决方案,也许是使用蒸汽的单衬里。但显然不是。无论如何,谢谢!
      【解决方案3】:

      试试这个!我假设您获得了响应,并且该响应保存在响应变量中

          var myarray = [String]()
          let hitDict = response["hits"] as! [String:AnyObject]
      
          let hitArray = hitDict["hits"] as! Array
      
          let someDict = hitArray[0] as! [String:AnyObject]
          let sourcDict = someDict["_source"] as! [String:AnyObject]
      
          let property1 = sourcDict["property1"] as! String
          let property2 = sourcDict["property2"] as! String
          myarray.append(property1)
          myarray.append(property2)
      

      【讨论】:

        【解决方案4】:
        var myArray = [String:String]()
        
        //response from try drop.client.get(…)
        let bodyReceived = responseFirebaseAssigned?.body.bytes
        
        //JSON object made of bodyReceived
        let JsonFirebase:JSON? 
        
             for val in JsonFirebase?.object ?? [:]{
        
                let valKey = val.key.string
                let valValue = val.value.string
                arrayFB[valKey!] = valValue
                print("arrayFB is \(arrayFB)")
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-03
          • 2020-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-17
          • 1970-01-01
          相关资源
          最近更新 更多