→ Fragment 에서 json 불러오기
context 객체사용
context.assets 하게되면, AssetsManager 이라는 객체를 참조하게 된다
불러온 InputStream 객체로, 데이터 read하고 String으로 변환!
// AssetLoader
package com.shoppi.app
import android.content.Context
class AssetLoader {
fun getJsonString(context: Context, fileName: String): String?{
return kotlin.runCatching {
loadAsset(context, fileName)
}.getOrNull()
}
private fun loadAsset(context: Context, fileName: String): String{
return context.assets.open(fileName).use { inputStream ->
val size = inputStream.available()
val bytes = ByteArray(size)
inputStream.read(bytes)
String(bytes)
}
}
}