繁体   English   中英

如何在Android中使用改造在Json响应中获取数组数据?

[英]How to get array data in Json response using retrofit in android?

嘿,我得到了一个包含字符串和数组的json响应,获取字符串工作正常,但是当尝试获取数组类型数据时,它会在Studio中显示错误。

{
"body": [
    {
       "customer": "erp touch",

      "description": [
        "ythn",
        "bgtr",
        "ythn"
    ]

  }
 ]
}

我可以获取客户,但无法进行描述,这是我用来描述的pojo

@SerializedName("description")
private List<String> description = null;

public List<String> getDescription() {
    return description;
}

这就是我用来得到它的东西

 OrderListResponse orderListResponse = response.body().getBody();

 description_tv.setText(orderListResponse.getDescription()); // this line give error cannot resolve setText(java.util.list<java.lang.string>)

注意:请不要与response.body()。getBody()混淆,因为我还没有发布完整的响应。

请告诉我如何获取此数据,任何帮助都将是可贵的。

谢谢!!

编辑

嘿,实际上我和我的伴侣弄清楚了我们想如何以数组形式显示此数据,而我对此有疑问。

我想从json响应中获取此描述数组,并在不同的textviews中显示其不同的元素。 使用,

description_tv1.setText(orderListResponse.getDescription().get(0));
description_tv2.setText(orderListResponse.getDescription().get(1));
description_tv3.setText(orderListResponse.getDescription().get(2));

将解决问题,但数组中的元素可以变化为任意数量,因此实际上我不知道我应该使用多少个textview,这是真正的问题。

有什么办法可以根据我的问题创建文本视图?

任何建议或帮助将不胜感激。

再次感谢 !

setText不接受List作为参数。 您可以尝试使用.join这样在列表中加入项目

String result = TextUtils.join(", ", orderListResponse.getDescription());

然后可以调用setText(result)


提示:确保您先检查结果和说明是否为空!

List<String> description = orderListResponse.getDescription();
if (description == null) { // show error }

尝试对TextView使用append

 for (String s : orderListResponse.getDescription()){
                description_tv.append(s);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM