如何从 json alamofire 在选择器视图中添加内容,我想选择 visitortypes 作为选择器视图,以便我想在数组中获取它
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"
}
]
}
这是我试过的:
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) ")
}
}
我必须将访问者类型放入一个数组中才能在选择器中显示
您可以为 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)
}
给你(看看你的 post 和评论,你说“ 实际上我想从这个 json 中获取数据到数组 "
如果您需要在不同的屏幕上或 class 轻松使用此数据。只需声明此
class myViewControoler: UIViewController {
struct communityVisitorTypeList
{
static var communityVisitorTypeListCount = 0
static var VisitorTypeID = [String]()
static var VisitorTypeImage = [String]()
static var VisitorType = [String()
}
}
现在在您的 Api 通话中,使用此
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
}
您可以在同一个 class 上使用这个数组,只需调用
communityVisitorTypeList.VisitorType
如果你想用不同的方式调用它 class/view
myViewControoler.communityVisitorTypeList.VisitorType
现在要从此数组中获取值,您可以编写任何函数并通过 communityVisitorTypeList.VisitorType[1] 或 2 或 3 或使用 for 循环调用!
设置你的数据源 n 委托
yourPicker.dataSource = 自己
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])
}
希望对您有所帮助。也请使用搜索功能。
"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) ")
}
}
我必须将访问者类型放入一个数组中才能在选择器中显示
您可以为 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)
}
给你(看看你的 post 和评论,你说“ 实际上我想从这个 json 中获取数据到数组 "
如果您需要在不同的屏幕上或 class 轻松使用此数据。只需声明此
class myViewControoler: UIViewController {
struct communityVisitorTypeList
{
static var communityVisitorTypeListCount = 0
static var VisitorTypeID = [String]()
static var VisitorTypeImage = [String]()
static var VisitorType = [String()
}
}
现在在您的 Api 通话中,使用此
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
}
您可以在同一个 class 上使用这个数组,只需调用
communityVisitorTypeList.VisitorType
如果你想用不同的方式调用它 class/view
myViewControoler.communityVisitorTypeList.VisitorType
现在要从此数组中获取值,您可以编写任何函数并通过 communityVisitorTypeList.VisitorType[1] 或 2 或 3 或使用 for 循环调用!
设置你的数据源 n 委托
yourPicker.dataSource = 自己 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])
}
希望对您有所帮助。也请使用搜索功能。