使用Kotlin读取大型(+ - 50MB)JSON文件
我开始使用“ OpenWeatherMap.org” API在天气应用程序上工作,它们为您提供JSON格式可用城市的列表。
在继续项目之前,我希望能够使用此JSON文件中的数据。
问题在于,每当我尝试阅读并解析该文件时,我都会变得无效。
这是代码:
主要活动:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val jsonFileString = getJsonDataFromAsset(applicationContext, "citylist.json")
Log.i("gabs Data", jsonFileString ?: "Empty Data")
val gson = Gson()
val listOfCities = object : TypeToken<List<CityList>>() {}.type
var cities: List<CityList> = gson.fromJson(jsonFileString, listOfCities)
cities.forEachIndexed { idx, city -> Log.i("data", "> Item $idx:\n$city") }
}
}
utils.kt:
fun getJsonDataFromAsset(context: Context, fileName: String): String? {
val jsonString: String
try {
jsonString = context.assets.open(fileName).bufferedReader().use { it.readText() }
} catch (ioException: IOException) {
ioException.printStackTrace()
return null
}
return jsonString
}
和数据类(城市数组数据):
class CityList : ArrayList<CityList.CityListItem>(){
data class CityListItem(
@SerializedName("coord")
val coord: Coord,
@SerializedName("country")
val country: String,
@SerializedName("id")
val id: Double,
@SerializedName("name")
val name: String,
@SerializedName("state")
val state: String
) {
data class Coord(
@SerializedName("lat")
val lat: Double,
@SerializedName("lon")
val lon: Double
)
}
}
错误:
java.lang.runtimeexception:无法启动活动componentInfo {com.example.weatherdisplay/com.example.weatherdisplay.weatherdisplay.weatherdisplay 。
引起的: 在com.example.weatherdisplay.ui.activities.mainactivity.oncreate(MainActivity.kt:21)
I'm starting to work on a weather app using "openweathermap.org" API, and they provide you with a list of available cities in Json format.
Before i continue with the project, i would like to able to work with the data from this Json file.
The problem is that i get Null whenever i try to read and parse that file.
Here is the code:
Main Activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val jsonFileString = getJsonDataFromAsset(applicationContext, "citylist.json")
Log.i("gabs Data", jsonFileString ?: "Empty Data")
val gson = Gson()
val listOfCities = object : TypeToken<List<CityList>>() {}.type
var cities: List<CityList> = gson.fromJson(jsonFileString, listOfCities)
cities.forEachIndexed { idx, city -> Log.i("data", "> Item $idx:\n$city") }
}
}
Utils.kt:
fun getJsonDataFromAsset(context: Context, fileName: String): String? {
val jsonString: String
try {
jsonString = context.assets.open(fileName).bufferedReader().use { it.readText() }
} catch (ioException: IOException) {
ioException.printStackTrace()
return null
}
return jsonString
}
And the data class (Array of cities data):
class CityList : ArrayList<CityList.CityListItem>(){
data class CityListItem(
@SerializedName("coord")
val coord: Coord,
@SerializedName("country")
val country: String,
@SerializedName("id")
val id: Double,
@SerializedName("name")
val name: String,
@SerializedName("state")
val state: String
) {
data class Coord(
@SerializedName("lat")
val lat: Double,
@SerializedName("lon")
val lon: Double
)
}
}
And the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weatherdisplay/com.example.weatherdisplay.ui.activities.MainActivity}: java.lang.NullPointerException: gson.fromJson(jsonFileString, listOfCities) must not be null.
Caused by: java.lang.NullPointerException: gson.fromJson(jsonFileString, listOfCities) must not be null
at com.example.weatherdisplay.ui.activities.MainActivity.onCreate(MainActivity.kt:21)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码中存在一些问题:
BufferedReader
我创建了一些与您的数据结构相对应的示例数据:
您的活动:
您的数据结构:
在最好的情况下,您会将获取文件内容的代码放在
viewModel
中,但这将超出此答案的范围。有关
ViewModels
的其他信息: https://developer.android。 com/topic/libraries/Architecture/ViewModelThere were some problems in your code:
BufferedReader
I created some sample data corresponding to your data structure:
Your activity:
Your data structure:
In the best case you would put the code in which you get the file contents and parse the data in a
ViewModel
, but this would to go beyond the scope for this answer.Additional information about
ViewModels
: https://developer.android.com/topic/libraries/architecture/viewmodel