如何在 Kotlin MVVM 数据绑定中解析 Json
How to parse Json in Kotlin MVVM Data binding
我正在尝试实现以下 Json 字符串:
{
"msg":[
"football",
"cricket",
"baseball",
"rugby",
"gulf"
],
"status":"success"
}
我创建了如下数据 classes:
class Sports(
val msg : List<String>,
val status : String
)
和
class Msg (
val football : List<String>,
val cricket : List<String>,
val baseball : List<String>,
val rugby : List<String>,
val gulf : List<String>
)
现在我正在尝试获取对象并按照教程在回收站视图列表中查看它。
我如何在下面更改它并在适配器中调用它?
interface PostApi {
/**
* Get the list of the pots from the API
*/
@GET("/posts")
fun getPosts(): Observable<List<Post>>
}
编辑:
我的适配器class如下:
class PostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>() {
private lateinit var postList:Sports
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder {
val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) {
holder.bind(postList)
}
override fun getItemCount(): Int {
//Getting error in .isInitialied 'Unresolved reference'
return if(::postList.isInitialized) postList.message.size else 0
}
fun updatePostList(postList: Sports){
this.postList = postList
notifyDataSetChanged()
}
class ViewHolder(private val binding:
ItemPostBinding):RecyclerView.ViewHolder(binding.root){ //Getting error in root 'Unresolved reference'
private val viewModel = PostViewModel()
fun bind(post: Sports){
viewModel.bind(post) //Getting error saying No value passed for parameter 'position'
binding.viewModel = viewModel
}
}
}
如果你从服务器得到 Json 然后像下面这样调用它:
interface SportsApi {
/**
* Get the Sports from the API
*/
@GET("/sports")
fun getSports(): Observable<Sports>
}
或者如果您想在本地签入,则必须转换此 Json
使用 Gson:
val sports = Gson().fromJson(json, Sports::java.class)
使用 Moshi:
val sports = Moshi.Builder().build().adapter(Sports::java.class).fromJson(json)
我正在尝试实现以下 Json 字符串:
{
"msg":[
"football",
"cricket",
"baseball",
"rugby",
"gulf"
],
"status":"success"
}
我创建了如下数据 classes:
class Sports(
val msg : List<String>,
val status : String
)
和
class Msg (
val football : List<String>,
val cricket : List<String>,
val baseball : List<String>,
val rugby : List<String>,
val gulf : List<String>
)
现在我正在尝试获取对象并按照教程在回收站视图列表中查看它。
我如何在下面更改它并在适配器中调用它?
interface PostApi {
/**
* Get the list of the pots from the API
*/
@GET("/posts")
fun getPosts(): Observable<List<Post>>
}
编辑:
我的适配器class如下:
class PostListAdapter: RecyclerView.Adapter<PostListAdapter.ViewHolder>() {
private lateinit var postList:Sports
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostListAdapter.ViewHolder {
val binding: ItemPostBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_post, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: PostListAdapter.ViewHolder, position: Int) {
holder.bind(postList)
}
override fun getItemCount(): Int {
//Getting error in .isInitialied 'Unresolved reference'
return if(::postList.isInitialized) postList.message.size else 0
}
fun updatePostList(postList: Sports){
this.postList = postList
notifyDataSetChanged()
}
class ViewHolder(private val binding:
ItemPostBinding):RecyclerView.ViewHolder(binding.root){ //Getting error in root 'Unresolved reference'
private val viewModel = PostViewModel()
fun bind(post: Sports){
viewModel.bind(post) //Getting error saying No value passed for parameter 'position'
binding.viewModel = viewModel
}
}
}
如果你从服务器得到 Json 然后像下面这样调用它:
interface SportsApi {
/**
* Get the Sports from the API
*/
@GET("/sports")
fun getSports(): Observable<Sports>
}
或者如果您想在本地签入,则必须转换此 Json
使用 Gson:
val sports = Gson().fromJson(json, Sports::java.class)
使用 Moshi:
val sports = Moshi.Builder().build().adapter(Sports::java.class).fromJson(json)