0
フィルタリングされたデータを配列形式に変換しようとしました。これはswift3の1行の配列を示しています。私の下のコードをチェックしてください。フィルタリングされたデータをswift3で配列形式に変換する方法は?
func serviceTest(){
let stringurl = "http://beta.json-generator.com/api/json/get/4JjQJ50O7"
let url = URL(string: stringurl)!
URLSession.shared.dataTask(with: url, completionHandler: {(data,response, error) in
guard data != nil else { return }
do{
var filterData:[String:Any] = [:]
var clusterData:[[String:Any]] = [[:]]
let json:[[String:Any]] = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [[String : Any]]
clusterData = json
var keyValue = ""
for cluster in clusterData
{
keyValue = cluster["Tag"] as! String
self.arr.append(keyValue)
var myArr = cluster["Feature"] as? AnyObject
var myA = [Any]()
myA = [myArr!]
var strArr = ""
for var i in 0...(myArr?.count)!-1 {
let m = myArr?[i] as AnyObject
let n = m["Name"] as AnyObject
let o = m["Tags"] as AnyObject
var oS = String(describing: o)
let nS = String(describing: n)
strArr = (nS as! String)+","+strArr
self.subArr = [strArr]
}
print("\(keyValue):\(self.subArr)")
print("---------------------")
self.sArr.append(self.subArr)
}
print("*********************************")
print("\(self.sArr)")
print("*********************************")
print("tcount:\(self.sArr.count)")
print(self.arr)
print("count:",self.arr.count)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}catch {
}
}).resume()
}
必要な出力:
Hybrid:["Moto",Lenovo","Infocus"]
---------------------
FingerPrint:["Kult","Redmi","Lenovo"]
---------------------
Security:["Nokia","Microsoft","Moto"]
---------------------
Cloud:["Microsoft","Samsung"]
---------------------
Analytics:["Lenovo"]
---------------------
私の出力:デビッドPá[email protected]
Hybrid:["Moto,Lenovo,Infocus,"]
---------------------
FingerPrint:["Kult,Redmi,Lenovo,"]
---------------------
Security:["Nokia,Microsoft,Moto,"]
---------------------
Cloud:["Microsoft,Samsung,"]
---------------------
Analytics:["Lenovo,"]
---------------------
可能な重複(https://stackoverflow.com/questions/42137285/split-string-into -substring-with-component-by-string-swift) –
'String.components(separatedBy:"、 ")'を使って、単一のStringから文字列の配列を取得するだけです。 –