【问题标题】:How to get a string array element from JSON?如何从 JSON 中获取字符串数组元素?
【发布时间】:2020-01-04 11:14:48
【问题描述】:

我有一个来自服务器的 JSON 文件,其中所有字段都是字符串。但是图像字段是一个字符串数组。如何让这个数组的所有元素显示在回收站视图中?

我的第一个 Json

{
  "_id": "5e1063a9688c775700025bcb",
  "firstName": "Armen",
  "lastName": "Mkhitaryan",
  "phone": 37493055062,
  "email": "armdev19@gmail.com",
  "notes": "Android Developer",
  "images": [
    "https://phonebookapp-683c.restdb.io/rest/images"
  ]
}

阵列图像

{
  "1": [
    "5e105996688c775700025b35",
    "5e105afd688c775700025b45",
    "5e105b11688c775700025b47",
    "5e105b2c688c775700025b4c",
    "5e105b35688c775700025b4e",
    "5e105b44688c775700025b50"
  ],
  "_id": "5e1065bf688c775700025bf4",
  "_created": "2020-01-04T10:15:27.375Z",
  "_changed": "2020-01-04T10:15:27.375Z",
  "_createdby": "maajob2012@gmail.com",
  "_changedby": "maajob2012@gmail.com",
  "_version": 0
}

我的模型

data class Contacts(

    @SerializedName("firstName")
    val firstName: String,
    @SerializedName("lastName")
    val lastName: String,
    @SerializedName("phone")
    val phone: String,
    @SerializedName("email")
    val email: String,
    @SerializedName("notes")
    val notes: String,
    @SerializedName("images")
    val images: List<String>
)

我的回收站适配器

class ContactsViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        var mContactIcon: CircleImageView = itemView.findViewById(R.id.contact_image)
        var mContactFirstName: TextView = itemView.findViewById(R.id.contact_first_name)
        var mContactPhone: TextView = itemView.findViewById(R.id.contact_phone)
        //var mContactLastName: TextView = itemView.findViewById(R.id.contact_last_name)

        fun bind(contactsModel: Contacts) {
            contactsModel.images.toString().let { url ->
                Picasso.with(itemView.context).load(url).placeholder(R.drawable.ic_person_placeholder)
                    .into(mContactIcon)
            }

            mContactFirstName.text = contactsModel.firstName
            mContactPhone.text = contactsModel.phone
        }
    }

【问题讨论】:

  • Array images 在这里做什么?没看懂,能说清楚吗?

标签: android arrays json kotlin


【解决方案1】:
contactsModel.images.let { url ->
            Picasso.with(itemView.context).load(url[0]).placeholder(R.drawable.ic_person_placeholder)
                .into(mContactIcon)
        }

试试这个

【讨论】:

  • 图像变量是否有价值
  • 不是 var,值 val
  • 以上的 JSON 不一样?
猜你喜欢
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
相关资源
最近更新 更多