【问题标题】:HOW to add contents in picker view from json alamofire ,I want to pick the visitortypes as picker view so that i want to fetch it in an array如何从 json alamofire 在选择器视图中添加内容,我想选择访问者类型作为选择器视图,以便我想在数组中获取它
【发布时间】:2020-04-02 09:13:03
【问题描述】:
"communityVisitorTypeList" : [
    {
      "VisitorTypeID" : 1,
      "VisitorTypeImage" : null,
      "VisitorType" : "HouseKeeping"
    },
    {
      "VisitorTypeID" : 2,
      "VisitorTypeImage" : null,
      "VisitorType" : "Driver"
    },
    {
      "VisitorTypeID" : 3,
      "VisitorTypeImage" : null,
      "VisitorType" : "Taxi"
    },
    {
      "VisitorTypeID" : 4,
      "VisitorTypeImage" : null,
      "VisitorType" : "Delivery"
    },
    {
      "VisitorTypeID" : 5,
      "VisitorTypeImage" : null,
      "VisitorType" : "Guest"
    },
    {
      "VisitorTypeID" : 6,
      "VisitorTypeImage" : null,
      "VisitorType" : "Service"
    }
  ]
} 

这是我尝试过的:

Alamofire.request(urltype, method: .get, parameters: parameters as Parameters, encoding: URLEncoding.default, headers:headers).responseJSON
{ (response ) in
    if(response.result.isSuccess) {
        let swiftyJsonVar = try? JSON(data: response.data!)
        print(swiftyJsonVar!)
        let resultarray = swiftyJsonVar!["responseData"].stringValue

        //This line of code covert String into JSON.
        let responseDataValue = JSON(parseJSON: resultarray)

        print( "this is:   \(responseDataValue) ")
    }
}

我必须在数组中获取访问者类型以在选择器中显示

【问题讨论】:

  • 欢迎来到stackoverflow。您应该向我们展示您迄今为止编写的代码....以便我们为您提供帮助。这不是一个“我们为您编写代码”的平台......
  • 编辑您的问题,使其具有 1) 简短但具有描述性的标题 2) 帖子正文中的实际问题,而不仅仅是标题 3) 一些代码显示您尝试过的内容
  • 首先将此 json 转换为 Dictionary 对象。然后使用下面的代码: let dictArray = dict["communityVisitorTypeList"] let visitorTypeArray = dictArray?.compactMap({ (value) -> String? in return value["VisitorType"] as? String })
  • 如何转换成字典对象?
  • @nidhinshiju 你昨天接受了我的回答,现在不接受了。似乎是什么问题?

标签: ios json swift parsing alamofire


【解决方案1】:

给你(看看你的帖子和 cmets,你说“实际上我想从这个 json 中获取数据到数组中

如果您需要在不同的屏幕或课程上轻松使用此数据。简单地声明这个

class myViewControoler: UIViewController {
struct communityVisitorTypeList
{
static var communityVisitorTypeListCount = 0
 static var VisitorTypeID = [String]()
static var VisitorTypeImage = [String]()
static var VisitorType = [String()

}


}

现在在你的 Api Call 上,使用这个

AF.request(urlString,method: .post, parameters: Parameters, encoding: URLEncoding.default, headers:headers).responseString{
switch response.result {
case let .success(value):
    let jsonData = value.data(using: .utf8)!
    let json = try? JSON(data: jsonData)
    let vistorTypeCount = json?["communityVisitorTypeList"].count
    communityVisitorTypeList.communityVisitorTypeListCount =  
    for i in (0..<vistorTypeCount)
    {
     communityVisitorTypeList.VisitorType.append(json?["vistorTypeCount"][i]["VisitorType").string ?? ""
  }
case let .failure(error):print(error);

    break
    }

你可以在同一个类上使用这个数组,只需调用

communityVisitorTypeList.VisitorType

如果你想在不同的类/视图中调用它

myViewControoler.communityVisitorTypeList.VisitorType

现在要从这个数组中获取值,您可以编写任何函数并通过 communityVisitorTypeList.VisitorType[1] 或 2 或 3 或使用 for 循环调用!

设置您的数据源 n 委托

yourPicker.dataSource = self yourPicker.delegate = 自我

现在调用你的函数

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    return communityVisitorTypeList.communityVisitorTypeListCount
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return communityVisitorTypeList. VisitorType[row]
}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
    print(communityVisitorTypeList. VisitorType[row])
}

希望对您有所帮助。也请使用搜索功能。

【讨论】:

    【解决方案2】:

    您可以为communityVisitorTypeList 创建一个模式。由于您使用swiftyJSON 来处理您的回复,您可以从您的回复中填充数组。根据填充的数组,您可以在pickerview 中显示数据。

    let resultarray = swiftyJsonVar!["responseData"]["communityVisitorTypeList"].arrayValue
    
    yourPicker.dataSource = self
    yourPicker.delegate = self
    

    以下是帮助您将数组中的值显示到pickerview的方法

        func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
            return 1
        }
    
        func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
            return yourArray.count
        }
    
        func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
            return yourArray[row].ELEMENT_FROM_YOUR_ARRAY
        }
    
        func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
            print(yourArray[row].ELEMENT_FROM_YOUR_ARRAY)
        }
    

    【讨论】:

    • 其实我想从这个 json 中获取数据到数组中
    • 你能把你的模态也发一下吗?
    • json 代码在问题上,先生,我得到响应的代码但我想将它提取到选择器(访问者类型)的数组中
    • 您已经编写了swiftyJsonVar!["responseData"].stringValue,但在您发布的代码中没有名为responseData 的键。你能发布完整的回复吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    相关资源
    最近更新 更多