-
创建assets资源文件
image.png
- 读取assets资源文件里面的json数据
//读取方法
fun getLogJson(context: Context, fileName: String): String? {
val stringBuilder = StringBuilder()
try {
val assetManager = context.assets
val bf =
BufferedReader(InputStreamReader(assetManager.open(fileName)))
var line: String?
while (bf.readLine().also { line = it } != null) {
stringBuilder.append(line)
}
} catch (e: IOException) {
e.printStackTrace()
}
return stringBuilder.toString()
}
使用阿里的fastjson进行解析
implementation 'com.alibaba:fastjson:1.2.53'
调用以上方法进去读取并转化为json
val json = SystemUtils.tools.getLogJson(this,"express.json") ?: ""
if (!TextUtils.isEmpty(json)){
val jsonEx = JSONObject(json)
val company = jsonEx.getString("zhongtong")
Log.e("tag",company)
}
网友评论