简体   繁体   English

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

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

Hey i got a json response which contain strings and arrays as well, fetching strings works fine but when tried to fetch array type data it gvies error in studio. 嘿,我得到了一个包含字符串和数组的json响应,获取字符串工作正常,但是当尝试获取数组类型数据时,它会在Studio中显示错误。

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

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

  }
 ]
}

I am able to fetch customer but could not do it for description, here is the pojo i am using for description 我可以获取客户,但无法进行描述,这是我用来描述的pojo

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

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

And this is what i am using to get it 这就是我用来得到它的东西

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

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

NOTE: Please do not get confused with response.body().getBody() because i haven't posted the complete response. 注意:请不要与response.body()。getBody()混淆,因为我还没有发布完整的响应。

Please tell me how to get this data any help will be appreciable. 请告诉我如何获取此数据,任何帮助都将是可贵的。

THANKS!! 谢谢!!

EDIT 编辑

Hey all actually i figured out with my mate that how we want to show this data in array, and i am havng problem with that. 嘿,实际上我和我的伴侣弄清楚了我们想如何以数组形式显示此数据,而我对此有疑问。

I want to fetch this description array from json response and show its different elements in different textviews. 我想从json响应中获取此描述数组,并在不同的textviews中显示其不同的元素。 Using , 使用,

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

will resolve the problem but elements in array can vary up to any number so in actual i dont know how many textviews i should use, this is the real problem. 将解决问题,但数组中的元素可以变化为任意数量,因此实际上我不知道我应该使用多少个textview,这是真正的问题。

Is there any way that i can create textviews according to my problems ? 有什么办法可以根据我的问题创建文本视图?

Any suggestion or help will appreciated. 任何建议或帮助将不胜感激。

THANKS AGAIN ! 再次感谢 !

setText does not accept a List as parameter. setText不接受List作为参数。 What you could try is to join the items in your list using .join like this 您可以尝试使用.join这样在列表中加入项目

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

And then you can call setText(result) 然后可以调用setText(result)


Just a tip: make sure you null check the result and description first! 提示:确保您先检查结果和说明是否为空!

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

Try using append with TextView 尝试对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