swift4.0 txt文件json取出转model

简介: func chooseTxtContent() -> Void {guardlet url = Bundle.main.url(forResource: "source/AllCountry/zh_Hans_CNCountry",withExt...

func chooseTxtContent() -> Void {
guard
let url = Bundle.main.url(
forResource: "source/AllCountry/zh_Hans_CNCountry",
withExtension: "txt") else {
return
}

    struct DataModel: Codable {
        let data: [CountryModel]?
    }
    
    do {
        let data = try Data(contentsOf: url)
        let model = try JSONDecoder().decode(DataModel.self, from: data)
        dataArray = model.data
    } catch {
        print(error)
    }
}

//
// CountryModel.swift
// chooseCountry
//
// Created by iOS on 2018/2/26.
// Copyright © 2018年 iOS. All rights reserved.
//

import UIKit

struct CountryModel : Codable {
let countryName : String?
let countryPinyin : String?
let phoneCode : String?
let countryCode : String?
}

目录
相关文章
|
13天前
|
JSON API 网络架构
【Azure 媒体服务】使用编码预设文件(Preset.json)来自定义编码任务 -- 创建视频缩略图
【Azure 媒体服务】使用编码预设文件(Preset.json)来自定义编码任务 -- 创建视频缩略图
|
1月前
|
JSON 数据格式 Python
【2023最新】Matlab 保存JSON数据集文件,并用Python读取
本文介绍了如何使用MATLAB生成包含数据和标签的JSON格式数据集文件,并展示了用Python读取该JSON文件作为训练集的方法。
55 1
|
11天前
|
存储 JSON JavaScript
|
17天前
|
JSON C语言 数据格式
Python导出隐马尔科夫模型参数到JSON文件C语言读取
Python导出隐马尔科夫模型参数到JSON文件C语言读取
13 1
|
13天前
|
JSON 数据格式 Python
【Azure Developer】Python 读取 json文件及过滤出需要的结果
【Azure Developer】Python 读取 json文件及过滤出需要的结果
|
15天前
|
JSON 数据格式
【应用服务 App Service】在Azure Web App的部署文件中,是否可以限制某些文件无法被访问?(如json)
【应用服务 App Service】在Azure Web App的部署文件中,是否可以限制某些文件无法被访问?(如json)
|
17天前
|
JSON Java 测试技术
单元测试问题之使用JSON文件作为参数化测试的输入源如何解决
单元测试问题之使用JSON文件作为参数化测试的输入源如何解决
37 0
|
1月前
|
JSON 数据格式 Python
python处理类似json的文件
python处理类似json的文件
|
2月前
|
JavaScript
Vue如何查看node版本---- package.json 文件中的 engines
Vue如何查看node版本---- package.json 文件中的 engines
|
2月前
|
JSON 数据格式
Content type ‘text/plain;charset=UTF-8‘ not supported,这里要把测试文件转为json格式
Content type ‘text/plain;charset=UTF-8‘ not supported,这里要把测试文件转为json格式
下一篇
DDNS