对数组的快速蒸气json响应

时间:2022-11-13 02:44:22

using swift vapor and elasticsearch, got a response like:

使用快速蒸汽和弹性搜索,得到了如下的响应:

{ "_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" }, ...

{ " _shards ":{“成功”,“失败”:0:5,“总”:5 },“打击”:{“点击率”:[{“_id”:“3”,“_index”:“items_v1”、“_score”:1.2029922,“_source”:{“property1”:“测试”、“property2”:“另一个测试”,…}, "_type": "item"},…

inside "hits" -> "hits" -> "_source" I got all the properties of my model "Item". How can I create an array of Items "[Item]" from this json response?

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

4 个解决方案

#1


0  

Parse your response in this way, so there will be no crashes if some value will not be sent.

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

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))
                }
            }
        }
    }
}

Int your Item class create init method, where you will initialize item's properties.

Int您的项目类创建init方法,在该方法中您将初始化项目的属性。

init(with dict: [String : Any]) {

    if let property1 = dict["property1"] as? Int {
        self.property1 = property1
    }

    super.init()
}

#2


1  

Small enhancement, use a guard statement to avoid the nested ifs...

小的增强,使用保护语句来避免嵌套的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))
   }
}

#3


0  

Try like this! I assume that you get the Response and that response in saved in response variable

这样的尝试!我假设你在响应变量中得到响应和那个响应

    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


0  

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)")
    }

#1


0  

Parse your response in this way, so there will be no crashes if some value will not be sent.

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

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))
                }
            }
        }
    }
}

Int your Item class create init method, where you will initialize item's properties.

Int您的项目类创建init方法,在该方法中您将初始化项目的属性。

init(with dict: [String : Any]) {

    if let property1 = dict["property1"] as? Int {
        self.property1 = property1
    }

    super.init()
}

#2


1  

Small enhancement, use a guard statement to avoid the nested ifs...

小的增强,使用保护语句来避免嵌套的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))
   }
}

#3


0  

Try like this! I assume that you get the Response and that response in saved in response variable

这样的尝试!我假设你在响应变量中得到响应和那个响应

    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


0  

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)")
    }