当前位置: 首页 > 知识库问答 >
问题:

如何将字符串响应转换为数组列表[重复]

拓拔稳
2023-03-14

我正在使用WebSocket,在onMessage()方法中,我收到了字符串形式的响应。我想把这个响应存储在模型类中。如何将这个字符串响应转换为数组列表?

[{"sender_id":"0008","created_at":"1313211849934","receiver_id":"0003","message":"second message","id":1,"chat_id":50,"type":"text"},{"sender_id":"0008","created_at":"1519211809934","receiver_id":"0003","message":"second message","id":2,"chat_id":50,"type":"text"}]

共有1个答案

贺飞
2023-03-14
class Data {
  var sender_id:String? = null
  var created_at:String? = null
  var receiver_id:String? = null
  var message:String? = null
  var id:Int
  var chat_id:Long
  var type:String? = null
}

val jsonContent = "[{"sender_id":"0008","created_at":"1313211849934","receiver_id":"0003","message":"second message","id":1,"chat_id":50,"type":"text"},{"sender_id":"0008","created_at":"1519211809934","receiver_id":"0003","message":"second message","id":2,"chat_id":50,"type":"text"}]"
val data = new Gson().fromJson(jsonContent, Data:class.java)
 类似资料: