我有Model类ModelWeeklyGuarantee想要将字符串arrayList转换为整数arrayList
示例[5,7,9]字符串arrayList,指向0的IntegerArrayList [5,7,8]。
public static ArrayList<Integer> listOfGuarantees= new ArrayList<Integer>();
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ModelWeeklyGuarantee ModelGuarantee = GauranteeList.get(position);
JSONArray jsonArray = ModelGuarantee.getWeeklyDataList();
Log.d("getWeeklyDataList",""+jsonArray);
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
try {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//Integer abc= Integer.valueOf(jsonObject.toString());
listOfGuarantees.add( Integer.valueOf(jsonObject.toString()));
}
catch (JSONException e) {
e.printStackTrace();
}
Log.d("guaranteevalues",""+listOfGuarantees);
}
getWeeklyDataList:[&#34; 5&#34;,&#34; 7&#34;,&#34; 9&#34;]
getWeeklyDataList:[&#34; 10&#34;,&#34; 11&#34;,&#34; 12&#34;]
guaranteevalues = []
org.json.JSONException:类型为java.lang.String的0处的值5无法转换为org.json.JSON.typeMismatch(JSON.java:100)中的JSONObject
答案 0 :(得分:1)
你不能把字符串保存到json对象。
替换此行
JSONObject jsonObject = jsonArray.getJSONObject(i);
listOfGuarantees.add( Integer.valueOf(jsonObject.toString()));
与
String obj = jsonArray.getString(i);
listOfGuarantees.add( Integer.valueOf(obj));
或者只需使用
listOfGuarantees.add(Integer.valueOf(jsonArray.getString(i)));
修改/更新强>
ArrayList<int[]> listOfGuarantees= new ArrayList<int[]>();
int[] values;
JSONArray jsonArray = ModelGuarantee.getWeeklyDataList();
Log.d("getWeeklyDataList",""+jsonArray);
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
values = new int[] { count };
try
{
String str = jsonArray.getString(i);
values[i] = Integer.valueOf(str);
}
catch (JSONException e) {
e.printStackTrace();
}
Log.d("guaranteevalues",""+listOfGuarantees);
}
listOfGuarantees.add(values);
希望有所帮助!