【问题标题】:How to get List<String> from JSON list using Retrofit 2?如何使用 Retrofit 2 从 JSON 列表中获取 List<String>?
【发布时间】:2017-06-05 06:39:31
【问题描述】:

我是 Android 新手。我需要从 JSON 列表中获取字符串列表。它将被添加到Spinner 列表中。我在服务器上有列表,例如

[{"bgroup":"A+"},{"bgroup":"A1+"},{"bgroup":"A1-"}]. 

尝试使用改造标量。 响应为

[{"bgroup":"A+"},{"bgroup":"A1+"},{"bgroup":"A1-"}]

但错误检测为:

应为字符串,但在第 1 行第 3 列路径 $[0]* 处为 BEGIN_OBJECT

有更好的方法从 JSON 中检索字符串列表吗?

【问题讨论】:

  • 它清楚地告诉您它无法从对象的来源读取字符串。为什么不直接创建一个映射以拥有List&lt;Mapping&gt; 并稍后直接从​​Mapping.bgroup 提取字符串?
  • 如果您对自定义数据对象使用相同的字符串,则改造会自动为您提供格式化的数据对象和值。所以使用相同的密钥。并使用依赖编译'com.squareup.retrofit2:converter-gson:2.0.2'
  • @LyubomyrShaydariv 感谢您的回答。你能给出这个场景的简单 List 例子吗?
  • @MdSal 这很简单:只需创建您自己的类似class Mapping { String bgroup; } 的东西并声明您的Retrofit 接口以返回List&lt;Mapping&gt;——然后您就可以轻松提取bgroup 属性。
  • @LyubomyrShaydariv 是的。我不想为字段创建模型类。 bcz 有多个字段,例如“bgroup”。到目前为止,我已经创建了模型及其工作。谢谢大家

标签: android json retrofit


【解决方案1】:

这是示例代码:

 GroupService groupService = createService(GroupService.class);

 Call<List<Groups>> groupCall = groupService.getGroups();

    groupCall.enqueue(new Callback<List<Groups>>() {
            @Override
            public void onResponse(retrofit.Response<List<Groups>> response, Retrofit retrofit) {


            }

            @Override
            public void onFailure(Throwable t) {
                t.printStackTrace();

            }
        });

接口:

public interface GroupService {

@GET("<URL>")
Call<List<Groups>> getGroups();
}

同时创建模型名称组。

我希望这对你有帮助。

【讨论】:

    【解决方案2】:

    制作如下模型类(parcelable)

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example implements Parcelable {
    
    @SerializedName("bgroup")
    @Expose
    private String bgroup;
    
    public String getBgroup() {
    return bgroup;
    }
    
    public void setBgroup(String bgroup) {
    this.bgroup = bgroup;
    }
    
    
        protected Example(Parcel in) {
            bgroup = in.readString();
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(bgroup);
        }
    
        @SuppressWarnings("unused")
        public static final Parcelable.Creator<Example> CREATOR = new Parcelable.Creator<Example>() {
            @Override
            public Example createFromParcel(Parcel in) {
                return new Example(in);
            }
    
            @Override
            public Example[] newArray(int size) {
                return new Example[size];
            }
        };
    }
    

    比创建这样的接口类

    public interface ApiService {
    
    @GET("<URL>")
    Call<List<Example>> getBloodGroups();
    }
    

    最后像下面这样调用改造:

    Call<List<Example>> call = new RestClient(this).getApiService()
                    .getBloodGroups();
            call.enqueue(new Callback<List<Example>>() {
                @Override
                public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
    
                }
    
                @Override
                public void onFailure(Call<List<Example>> call, Throwable throwable) {
    
                }
            });
    

    【讨论】:

    • 是的。你的答案应该有效。但我想在没有模型类的情况下使用。请问还有其他简单的方法吗?
    • 所以您只想以 JSON 字符串的形式获得响应??
    • 我想将响应值添加到 List,然后我可以使用微调器下拉菜单。最后我创建了简单的模型类。它现在工作。谢谢@Krishna Meena
    猜你喜欢
    • 2023-03-28
    • 2016-11-18
    • 2012-06-08
    • 2017-09-15
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多