首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析本地JSON数据

解析本地JSON数据
EN

Stack Overflow用户
提问于 2020-10-31 04:06:51
回答 1查看 48关注 0票数 0

示例JSON格式:

代码语言:javascript
运行
复制
[
    {
        "city": "Tokyo", 
        "admin": "T\u014dky\u014d", 
        "country": "Japan", 
        "population_proper": "8336599", 
        "iso2": "JP", 
        "capital": "primary", 
        "lat": "35.685", 
        "lng": "139.751389", 
        "population": "35676000"
    }, 
    {
        "city": "\u014csaka", 
        "admin": "\u014csaka", 
        "country": "Japan", 
        "population_proper": "2592413", 
        "iso2": "JP", 
        "capital": "admin", 
        "lat": "34.683333", 
        "lng": "135.516667", 
        "population": "11294000"
    }
]

试着让标题出现在山顶上,但没有成功。

代码语言:javascript
运行
复制
    struct JapCity: Codable {
        var city: String
        var admin: String
        var country: String
        var population: String
        var lat: String
        var lng: String
    }
    
    var japcityinfo = [JapCity]()
    
    
    @IBOutlet weak var cityPicker: UIPickerView!

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        cityPicker.delegate = self
        cityPicker.dataSource = self
        
        jasonCall()
        
        cityPicker.reloadAllComponents()
        
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return japcityinfo.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        print("here")
        return japcityinfo[row].city
    }
    
    
    func jasonCall() {
        let decoder = JSONDecoder()

        do {
            //let jsonData = Data(jsonString.utf8)
            let path = Bundle.main.path(forResource: "jp", ofType: "json")
            guard path != nil else {
                print("Error: Can't find JSON file")
                return
            }
            let url = URL(fileURLWithPath: path!)
            let data = try Data(contentsOf: url)
            
            let japancity = try decoder.decode([JapCity].self, from: data)
            
            print(japancity)
            print(japancity.count)
            let citydataNum = japancity.count
            print(citydataNum)
            
        } catch {
            print(error.localizedDescription)
        }
        
    }

}

有人能帮忙吗?

EN

Stack Overflow用户

回答已采纳

发布于 2020-10-31 04:46:09

您必须在解析json文件中的数据后填充japcityinfo数组,然后重新加载cityPicker以使它们得到反映。

在打印语句之后添加以下两行:

代码语言:javascript
运行
复制
self.japcityinfo = japancity
self.cityPicker.reloadAllComponents()

因此,您的jasonCall函数将如下所示:

代码语言:javascript
运行
复制
 func jasonCall() {
        let decoder = JSONDecoder()

        do {
            //let jsonData = Data(jsonString.utf8)
            let path = Bundle.main.path(forResource: "jp", ofType: "json")
            guard path != nil else {
                print("Error: Can't find JSON file")
                return
            }
            let url = URL(fileURLWithPath: path!)
            let data = try Data(contentsOf: url)
            
            let japancity = try decoder.decode([JapCity].self, from: data)
            
            print(japancity)
            print(japancity.count)
            let citydataNum = japancity.count
            print(citydataNum)
            
            self.japcityinfo = japancity
            self.cityPicker.reloadAllComponents()
            
        } catch {
            print(error.localizedDescription)
        }
        
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64618316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档