Search code examples
iosjsonswiftparsingalamofire

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


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

This is what I have tried:

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

I have to get the visitor type in an array to show in picker


Solution

  • Here you go (looking at your post and comments, you said " Actualy i want to fetch the data from this json to the array "

    If you need to use this data on a different screen or class easily. Simply declare this

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

    Now on your Api Call , use this

    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
        }
    
    

    You can use this array on the same class by simply calling

    communityVisitorTypeList.VisitorType
    

    and if you want to call it in a different class/view

    myViewControoler.communityVisitorTypeList.VisitorType
    

    Now to fetch values from this array, you could write any function and call by communityVisitorTypeList.VisitorType[1] or 2 or 3 or with for loop !

    set your datasource n delegate

    yourPicker.dataSource = self yourPicker.delegate = self

    now to call your functions

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

    Hope it helps. Also please use search function.