开发者社区> 问答> 正文

从JsonArray中仅选择某些键值

我有一个JsonArray,如下所示,并且我正在使用改型发送GET请求。我只想要name和description来自每个JsonObjects。

我得到的错误是 E/Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

我应该如何只获取所需的键值?

{
    "data": [
        {
            "id": "GEnAad",
            "name": "test project",
            "description": "app",
            "avatar": null,
            "disabled": 0,
            "custom_settings": null,
            "created_at": "2019-10-14 05:59:41",
            "updated_at": "2019-10-14 05:59:41",         

        },
        {
            "id": "OEypaY",
            "name": "Demo project",
            "description": "IBIS V0.25 demo",
            "avatar": null,
            "disabled": 0,
            "custom_settings": null,
            "created_at": "2019-10-07 05:26:43",
            "updated_at": "2019-10-10 09:56:24",           
        },
        {
            "id": "Oaw0ER",
            "name": "helloworld",
            "description": "helloworld",
            "avatar": null,
            "disabled": 0,
            "custom_settings": null,
            "created_at": "2019-11-28 07:39:37",
            "updated_at": "2019-11-28 07:39:37",           
        }
    ]
}

GET请求:

public interface IbisRepo {

    @Headers({"Accept: application/json"})
    @GET("/api/projects")
    Call<List<ProjectInformation>> getProjectInfo(
            @Header("Authorization") String accessToken);
}
ProjectInformation.java:

public class ProjectInformation {

    @SerializedName("name")
    private String Name;

    @SerializedName("description")
    private String description;

    public String getName() {
        return Name;
    }

    public String getDescription() {
        return description;
    }
}

MainActivity.java

    private ListView listView;
    private String accessToken = "accesstoken"
    private String API_BASE_URL = "API_BASE_URL"
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById (R.id.pagination_list);

        sendNetworkRequest(accessToken);
    }

Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.build();

        IbisRepo client = retrofit.create(IbisRepo.class);
        Call<List<ProjectInformation>> call = client.getProjectInfo(accessToken);

        call.enqueue(new Callback<List<ProjectInformation>>() {
            @Override
            public void onResponse(Call<List<ProjectInformation>> call, Response<List<ProjectInformation>> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(MainActivity.this, "Code: " + response.code(), Toast.LENGTH_SHORT).show();
                    return;
                }
                List<ProjectInformation> repos = response.body();
                listView.setAdapter(new IbisRepoAdapter(MainActivity.this, repos));
                Log.d("Success", "onResponse: Hello World");
                Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<List<ProjectInformation>> call, Throwable t) {
                    Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

展开
收起
Puppet 2019-12-05 13:13:59 358 0
1 条回答
写回答
取消 提交回答
  • 只需更改您的pojo课:)

    
    public class VoLoginResponse implements Serializable {
    
    private VoLoginData data;
    
    public VoLoginData getData() {
        return data;
    }
    
    public void setData(VoLoginData data) {
        this.data = data;
    }
    
    public class VoLoginData implements Serializable {
        String name;
        String description;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    }
      }
    
    2019-12-05 13:14:40
    赞同 展开评论 打赏
问答分类:
API
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载