如何将 JsonArray 对象转换为要在微调器 java android 上使用的字符串数组?
How to convert JsonArray object to Array of string to be used on spinner java android?
我创建了一个 API,(查看图片)
我需要从 api 获取 sumber[] 并将其存储到我的微调器中。但是我得到了错误:
有什么最佳实践方法可以将 jsonArray 对象投射到微调器吗?
java.lang.ArrayStoreException: source[0] of type Item_sumber cannot be stored in destination array of type java.lang.String[]
Activity_addcontact.java
private ArrayList Arraylist_sumber, Arraylist_budget;
....
Call<Formdata> call = jsonPlaceHolderApi.getFormdata();
call.enqueue(new Callback<Formdata>() {
@Override
public void onResponse(Call<Formdata> call, Response<Formdata> response) {
Formdata jsonResponse = response.body();
String[] string_sumber = Arrays.copyOf(jsonResponse.getSumber(), jsonResponse.getSumber().length,String[].class);
ArrayAdapter<String> adapter_sumber = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, string_sumber);
dropdown_sumber.setAdapter(adapter_sumber);
dropdown_sumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
id_sumber = String.valueOf(i + 1);
}
Item_sumber
private String id;
private String nama_sumber;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNama_sumber() {
return nama_sumber;
}
public void setNama_sumber(String nama_sumber) {
this.nama_sumber = nama_sumber;
}
Formdata.java
public class Formdata {
private Item_sumber[] sumber;
private Item_budget[] budget;
public Item_sumber[] getSumber() {
return sumber;
}
public void setSumber(Item_sumber[] sumber) {
this.sumber = sumber;
}
public Item_budget[] getBudget() {
return budget;
}
public void setBudget(Item_budget[] budget) {
this.budget = budget;
}
}
您可以使用 Jackson 库的 ObjectMapper class。
它将转换来自对象 API 的响应,
语法--
objectMapper om = new ObjectMapper();
om.readValue(response.body().asString(),FormData[].class);
我想,这对你有帮助。
您正在将 'sumber' 数组中的对象元素存储在字符串数组 'string_sumber' 中。
试试这个
JSONArray sumberArray = jsonresponse.getJsonArray("sumber");
String[] string_sumber = new String[sumberArray.length];
for(int i=0;i<sumberArray.length;i++){
string_summber[i] = get(i).toString();
}
朋友帮我解决的,我已经解决了
制作并转换 jsonarray 为 Arraylist(对象)
进行迭代以便将其存储到字符串数组[]
完成!现在将它投射到你的微调器
Call<Formdata> call = jsonPlaceHolderApi.getFormdata();
call.enqueue(new Callback<Formdata>() {
@Override
public void onResponse(Call<Formdata> call, Response<Formdata> response) {
Formdata jsonResponse = response.body();
ArrayList<Item_sumber> arraylist_sumber = new ArrayList<>(Arrays.asList(jsonResponse.getSumber()));
ArrayList<String> list_sumber = new ArrayList<>();
for (int i = 0; i < jsonResponse.getSumber().length; i++) {
Item_sumber item_sumber = arraylist_sumber.get(i);
list_sumber.add(item_sumber.getId());
}
string_sumber = new String[arraylist_sumber.size()];
/* If 2 dimensional array convert
private String[][] string_sumber2, string_budget2;
string_sumber2 = new String[arraylist_sumber.size()][6];
for (int i = 0; i < jsonResponse.getSumber().length; i++) {
Item_sumber data = arraylist_sumber.get(i);
string_sumber2[i] = new String[]{data.getId(),data.getNama_sumber()};
}*/
for (int i = 0; i < jsonResponse.getSumber().length; i++) {
Item_sumber data = arraylist_sumber.get(i);
string_sumber[i] = data.getNama_sumber();
}
ArrayAdapter<String> adapter_sumber = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, string_sumber);
dropdown_sumber.setAdapter(adapter_sumber);
dropdown_sumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
id_sumber = String.valueOf(i + 1);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
@Override
public void onFailure(Call<Formdata> call, Throwable t) {
call.cancel();
}
});
有什么最佳实践方法可以将 jsonArray 对象投射到微调器吗?
java.lang.ArrayStoreException: source[0] of type Item_sumber cannot be stored in destination array of type java.lang.String[]
Activity_addcontact.java
private ArrayList Arraylist_sumber, Arraylist_budget;
....
Call<Formdata> call = jsonPlaceHolderApi.getFormdata();
call.enqueue(new Callback<Formdata>() {
@Override
public void onResponse(Call<Formdata> call, Response<Formdata> response) {
Formdata jsonResponse = response.body();
String[] string_sumber = Arrays.copyOf(jsonResponse.getSumber(), jsonResponse.getSumber().length,String[].class);
ArrayAdapter<String> adapter_sumber = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, string_sumber);
dropdown_sumber.setAdapter(adapter_sumber);
dropdown_sumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
id_sumber = String.valueOf(i + 1);
}
Item_sumber
private String id;
private String nama_sumber;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNama_sumber() {
return nama_sumber;
}
public void setNama_sumber(String nama_sumber) {
this.nama_sumber = nama_sumber;
}
Formdata.java
public class Formdata {
private Item_sumber[] sumber;
private Item_budget[] budget;
public Item_sumber[] getSumber() {
return sumber;
}
public void setSumber(Item_sumber[] sumber) {
this.sumber = sumber;
}
public Item_budget[] getBudget() {
return budget;
}
public void setBudget(Item_budget[] budget) {
this.budget = budget;
}
}
您可以使用 Jackson 库的 ObjectMapper class。 它将转换来自对象 API 的响应, 语法--
objectMapper om = new ObjectMapper();
om.readValue(response.body().asString(),FormData[].class);
我想,这对你有帮助。
您正在将 'sumber' 数组中的对象元素存储在字符串数组 'string_sumber' 中。
试试这个
JSONArray sumberArray = jsonresponse.getJsonArray("sumber");
String[] string_sumber = new String[sumberArray.length];
for(int i=0;i<sumberArray.length;i++){
string_summber[i] = get(i).toString();
}
朋友帮我解决的,我已经解决了
制作并转换 jsonarray 为 Arraylist(对象)
进行迭代以便将其存储到字符串数组[]
完成!现在将它投射到你的微调器
Call<Formdata> call = jsonPlaceHolderApi.getFormdata(); call.enqueue(new Callback<Formdata>() { @Override public void onResponse(Call<Formdata> call, Response<Formdata> response) { Formdata jsonResponse = response.body(); ArrayList<Item_sumber> arraylist_sumber = new ArrayList<>(Arrays.asList(jsonResponse.getSumber())); ArrayList<String> list_sumber = new ArrayList<>(); for (int i = 0; i < jsonResponse.getSumber().length; i++) { Item_sumber item_sumber = arraylist_sumber.get(i); list_sumber.add(item_sumber.getId()); } string_sumber = new String[arraylist_sumber.size()]; /* If 2 dimensional array convert private String[][] string_sumber2, string_budget2; string_sumber2 = new String[arraylist_sumber.size()][6]; for (int i = 0; i < jsonResponse.getSumber().length; i++) { Item_sumber data = arraylist_sumber.get(i); string_sumber2[i] = new String[]{data.getId(),data.getNama_sumber()}; }*/ for (int i = 0; i < jsonResponse.getSumber().length; i++) { Item_sumber data = arraylist_sumber.get(i); string_sumber[i] = data.getNama_sumber(); } ArrayAdapter<String> adapter_sumber = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, string_sumber); dropdown_sumber.setAdapter(adapter_sumber); dropdown_sumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { id_sumber = String.valueOf(i + 1); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); @Override public void onFailure(Call<Formdata> call, Throwable t) { call.cancel(); } });