Android - 存、取、读 本地 Json 文件
2023-03-16 本文已影响0人
因为我的心
一、前言:
有时候,我们需要把一下json数据放到APP内部来,我们就需要把json格式的数据存到本地,需要的时候去读取。
二、实现:
1、准备一个txt文件,命名为code.txt,把JSON格式的数据放入code.txt文件中
图片.png2、修改后缀名为.json,更改编码格式为 UTF-8
图片.png3、新建一个 assets 文件夹,将我们生成的json文件放在其内即可
图片.png4、读取本地数据
/**
* 读取世界各国的区号
*/
inline fun readWorldAreaCode(context: Context){
var br: BufferedReader? = null
var fileName = "code.json"
try {
br = BufferedReader(InputStreamReader(context.resources.assets.open(fileName)))
var line: String? = null
val sb = StringBuilder()
while (br.readLine().also { line = it } != null){
sb.append(line)
}
br.close()
var json = sb.toString()
MyToash.Log("lyy","---json: $json")
val codeList = JSON.parseArray(json, WorldAreaCode::class.java)
codeList.forEach {
//打印数据
MyToash.Log("lyy","---county: ${it.county} code:${it.code}")
}
} catch (e: Exception) {
MyToash.Log("lyy","---e: "+e.toString())
}finally {
br?.close()
}
}
打印数据:
---county: 安哥拉 code:244
---county: 阿富汗 code:93
---county: 阿尔巴尼亚 code:355
---county: 阿尔及利亚 code:213
---county: 安道尔共和国 code:376*/
注意:解析的时候报错:
com.alibaba.fastjson.JSONException: default constructor not found.
修改fastjson版本号即可:
implementation 'com.alibaba:fastjson:1.2.32'
5、WorldAreaCode
/**
* 数据对象
*/
data class WorldAreaCode(
var code: Int? = 86,//": 86,
var county: String? = "中国"// "中国"
)
6、code.json数据源:
[
{
"code": 86,
"county": "中国"
},
{
"code": 244,
"county": "安哥拉"
},
{
"code": 93,
"county": "阿富汗"
},
{
"code": 355,
"county": "阿尔巴尼亚"
},
{
"code": 213,
"county": "阿尔及利亚"
},
{
"code": 376,
"county": "安道尔共和国"
},
{
"code": 1264,
"county": "安圭拉岛"
},
{
"code": 1268,
"county": "安提瓜和巴布达"
},
{
"code": 54,
"county": "阿根廷"
},
{
"code": 374,
"county": "亚美尼亚"
},
{
"code": 247,
"county": "阿森松"
},
{
"code": 61,
"county": "澳大利亚"
},
{
"code": 43,
"county": "奥地利"
},
{
"code": 994,
"county": "阿塞拜疆"
},
{
"code": 1242,
"county": "巴哈马"
},
{
"code": 973,
"county": "巴林"
},
{
"code": 880,
"county": "孟加拉国"
},
{
"code": 1246,
"county": "巴巴多斯"
},
{
"code": 375,
"county": "白俄罗斯"
},
{
"code": 32,
"county": "比利时"
},
{
"code": 501,
"county": "伯利兹"
},
{
"code": 229,
"county": "贝宁"
},
{
"code": 1441,
"county": "百慕大群岛"
},
{
"code": 591,
"county": "玻利维亚"
},
{
"code": 267,
"county": "博茨瓦纳"
},
{
"code": 55,
"county": "巴西"
},
{
"code": 673,
"county": "文莱"
},
{
"code": 359,
"county": "保加利亚"
},
{
"code": 226,
"county": "布基纳法索"
},
{
"code": 95,
"county": "缅甸"
},
{
"code": 257,
"county": "布隆迪"
},
{
"code": 237,
"county": "喀麦隆"
},
{
"code": 1,
"county": "加拿大"
},
{
"code": 1345,
"county": "开曼群岛"
},
{
"code": 236,
"county": "中非共和国"
},
{
"code": 235,
"county": "乍得"
},
{
"code": 56,
"county": "智利"
},
{
"code": 57,
"county": "哥伦比亚"
},
{
"code": 242,
"county": "刚果"
},
{
"code": 682,
"county": "库克群岛"
},
{
"code": 506,
"county": "哥斯达黎加"
},
{
"code": 53,
"county": "古巴"
},
{
"code": 357,
"county": "塞浦路斯"
},
{
"code": 420,
"county": "捷克"
},
{
"code": 45,
"county": "丹麦"
},
{
"code": 253,
"county": "吉布提"
},
{
"code": 1890,
"county": "多米尼加共和国"
},
{
"code": 593,
"county": "厄瓜多尔"
},
{
"code": 20,
"county": "埃及"
},
{
"code": 503,
"county": "萨尔瓦多"
},
{
"code": 372,
"county": "爱沙尼亚"
},
{
"code": 251,
"county": "埃塞俄比亚"
},
{
"code": 679,
"county": "斐济"
},
{
"code": 358,
"county": "芬兰"
},
{
"code": 33,
"county": "法国"
},
{
"code": 594,
"county": "法属圭亚那"
},
{
"code": 241,
"county": "加蓬"
},
{
"code": 220,
"county": "冈比亚"
},
{
"code": 995,
"county": "格鲁吉亚"
},
{
"code": 49,
"county": "德国"
},
{
"code": 233,
"county": "加纳"
},
{
"code": 350,
"county": "直布罗陀"
},
{
"code": 30,
"county": "希腊"
},
{
"code": 1809,
"county": "格林纳达"
},
{
"code": 1671,
"county": "关岛"
},
{
"code": 502,
"county": "危地马拉"
},
{
"code": 224,
"county": "几内亚"
},
{
"code": 592,
"county": "圭亚那"
},
{
"code": 509,
"county": "海地"
},
{
"code": 504,
"county": "洪都拉斯"
},
{
"code": 852,
"county": "中国香港"
},
{
"code": 36,
"county": "匈牙利"
},
{
"code": 354,
"county": "冰岛"
},
{
"code": 91,
"county": "印度"
},
{
"code": 62,
"county": "印度尼西亚"
},
{
"code": 98,
"county": "伊朗"
},
{
"code": 964,
"county": "伊拉克"
},
{
"code": 353,
"county": "爱尔兰"
},
{
"code": 972,
"county": "以色列"
},
{
"code": 39,
"county": "意大利"
},
{
"code": 225,
"county": "科特迪瓦"
},
{
"code": 1876,
"county": "牙买加"
},
{
"code": 81,
"county": "日本"
},
{
"code": 962,
"county": "约旦"
},
{
"code": 855,
"county": "柬埔寨"
},
{
"code": 327,
"county": "哈萨克斯坦"
},
{
"code": 254,
"county": "肯尼亚"
},
{
"code": 82,
"county": "韩国"
},
{
"code": 965,
"county": "科威特"
},
{
"code": 331,
"county": "吉尔吉斯坦"
},
{
"code": 856,
"county": "老挝"
},
{
"code": 371,
"county": "拉脱维亚"
},
{
"code": 961,
"county": "黎巴嫩"
},
{
"code": 266,
"county": "莱索托"
},
{
"code": 231,
"county": "利比里亚"
},
{
"code": 218,
"county": "利比亚"
},
{
"code": 423,
"county": "列支敦士登"
},
{
"code": 370,
"county": "立陶宛"
},
{
"code": 352,
"county": "卢森堡"
},
{
"code": 853,
"county": "中国澳门"
},
{
"code": 261,
"county": "马达加斯加"
},
{
"code": 265,
"county": "马拉维"
},
{
"code": 60,
"county": "马来西亚"
},
{
"code": 960,
"county": "马尔代夫"
},
{
"code": 223,
"county": "马里"
},
{
"code": 356,
"county": "马耳他"
},
{
"code": 1670,
"county": "马里亚那群岛"
},
{
"code": 596,
"county": "马提尼克"
},
{
"code": 230,
"county": "毛里求斯"
},
{
"code": 52,
"county": "墨西哥"
},
{
"code": 373,
"county": "摩尔多瓦"
},
{
"code": 377,
"county": "摩纳哥"
},
{
"code": 976,
"county": "蒙古"
},
{
"code": 1664,
"county": "蒙特塞拉特岛"
},
{
"code": 212,
"county": "摩洛哥"
},
{
"code": 258,
"county": "莫桑比克"
},
{
"code": 264,
"county": "纳米比亚"
},
{
"code": 674,
"county": "瑙鲁"
},
{
"code": 977,
"county": "尼泊尔"
},
{
"code": 599,
"county": "荷属安的列斯"
},
{
"code": 31,
"county": "荷兰"
},
{
"code": 64,
"county": "新西兰"
},
{
"code": 505,
"county": "尼加拉瓜"
},
{
"code": 227,
"county": "尼日尔"
},
{
"code": 234,
"county": "尼日利亚"
},
{
"code": 850,
"county": "朝鲜"
},
{
"code": 47,
"county": "挪威"
},
{
"code": 968,
"county": "阿曼"
},
{
"code": 92,
"county": "巴基斯坦"
},
{
"code": 507,
"county": "巴拿马"
},
{
"code": 675,
"county": "巴布亚新几内亚"
},
{
"code": 595,
"county": "巴拉圭"
},
{
"code": 51,
"county": "秘鲁"
},
{
"code": 63,
"county": "菲律宾"
},
{
"code": 48,
"county": "波兰"
},
{
"code": 689,
"county": "法属玻利尼西亚"
},
{
"code": 351,
"county": "葡萄牙"
},
{
"code": 1787,
"county": "波多黎各"
},
{
"code": 974,
"county": "卡塔尔"
},
{
"code": 262,
"county": "留尼旺"
},
{
"code": 40,
"county": "罗马尼亚"
},
{
"code": 7,
"county": "俄罗斯"
},
{
"code": 1758,
"county": "圣卢西亚"
},
{
"code": 1784,
"county": "圣文森特岛"
},
{
"code": 684,
"county": "东萨摩亚(美)"
},
{
"code": 685,
"county": "西萨摩亚"
},
{
"code": 378,
"county": "圣马力诺"
},
{
"code": 239,
"county": "圣多美和普林西比"
},
{
"code": 966,
"county": "沙特阿拉伯"
},
{
"code": 221,
"county": "塞内加尔"
},
{
"code": 248,
"county": "塞舌尔"
},
{
"code": 232,
"county": "塞拉利昂"
},
{
"code": 65,
"county": "新加坡"
},
{
"code": 421,
"county": "斯洛伐克"
},
{
"code": 386,
"county": "斯洛文尼亚"
},
{
"code": 677,
"county": "所罗门群岛"
},
{
"code": 252,
"county": "索马里"
},
{
"code": 27,
"county": "南非"
},
{
"code": 34,
"county": "西班牙"
},
{
"code": 94,
"county": "斯里兰卡"
},
{
"code": 1758,
"county": "圣卢西亚"
},
{
"code": 1784,
"county": "圣文森特"
},
{
"code": 249,
"county": "苏丹"
},
{
"code": 597,
"county": "苏里南"
},
{
"code": 268,
"county": "斯威士兰"
},
{
"code": 46,
"county": "瑞典"
},
{
"code": 41,
"county": "瑞士"
},
{
"code": 963,
"county": "叙利亚"
},
{
"code": 886,
"county": "中国台湾"
},
{
"code": 992,
"county": "塔吉克斯坦"
},
{
"code": 255,
"county": "坦桑尼亚"
},
{
"code": 66,
"county": "泰国"
},
{
"code": 228,
"county": "多哥"
},
{
"code": 676,
"county": "汤加"
},
{
"code": 1809,
"county": "特立尼达和多巴哥"
},
{
"code": 216,
"county": "突尼斯"
},
{
"code": 90,
"county": "土耳其"
},
{
"code": 993,
"county": "土库曼斯坦"
},
{
"code": 256,
"county": "乌干达"
},
{
"code": 380,
"county": "乌克兰"
},
{
"code": 971,
"county": "阿拉伯联合酋长国"
},
{
"code": 44,
"county": "英国"
},
{
"code": 1,
"county": "美国"
},
{
"code": 598,
"county": "乌拉圭"
},
{
"code": 233,
"county": "乌兹别克斯坦"
},
{
"code": 58,
"county": "委内瑞拉"
},
{
"code": 84,
"county": "越南"
},
{
"code": 967,
"county": "也门"
},
{
"code": 381,
"county": "南斯拉夫"
},
{
"code": 263,
"county": "津巴布韦"
},
{
"code": 243,
"county": "扎伊尔"
},
{
"code": 260,
"county": "赞比亚"
}
]
参考:https://blog.csdn.net/qq_20451879/article/details/126020170