[英]How to convert json object from json array to string array in android
My JSON is我的 JSON 是
[
{
"Heading": "Heading 1"
},
{
"Heading": "Heading 2"
},
{
"Heading": "Heading 3"
}
]
I want this array is converted into我想把这个数组转换成
String heading[]={"Heading 1","Heading 2","Heading 3"};
I am using retrofit for load jsonarray.我正在使用改造来加载 jsonarray。 this json link is http://www.mocky.io/v2/5e6f3b37330000a11df077ce
这个 json 链接是http://www.mocky.io/v2/5e6f3b37330000a11df077ce
just at the api interface make it returns ArrayList只是在 api 接口使它返回 ArrayList
@GET("http://www.mocky.io/v2/{itemId}")
Call<ArrayList<Item>> getItem(@Path("itemId") String id)
or if you are using RxJava change to或者如果您使用 RxJava 更改为
@GET("http://www.mocky.io/v2/{itemId}")
Single<ArrayList<Item>> getItem(@Path("itemId") String id)
Class Item{
private String Heading;
//add the getters and setters
}
Use this code to solve your problem.使用此代码来解决您的问题。
public void getItemList(String id)
{
ApiConfig config = RetrofitClient.getRetrofitInstance().create(ApiConfig.class);
Call<List<ItemsModel>> call = config.getItmeList(id);
call.enqueue(new Callback<List<ItemsModel>>() {
@SuppressLint("SetTextI18n")
@Override
public void onResponse(@NonNull Call<List<ItemsModel>> call, @NonNull Response<List<ItemsModel>> response) {
if (response.isSuccessful()) { // response is successful
if(response.body() != null)
{
String[] items = response.body().toArray(new String[0]);//this is string array
}
}
}
@Override
public void onFailure(@NonNull Call<List<ItemsModel>> call, @NonNull Throwable t) {
Log.d("errorResponse", Objects.requireNonNull(t.getMessage()));
}
});
}
Item pojo class项目 pojo 类
public class ItemsModel
{
@SerializedName("heading")
private String heading;
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
}
Interface界面
@GET("http://www.mocky.io/v2/{itemId}")
Call<List<ItemsModel>> getItmeList(@Path("itemId") String id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.