【发布时间】:2017-11-13 08:39:38
【问题描述】:
我有 Model 类 ModelWeeklyGuarantee 想要将字符串 arrayList 转换为整数 arrayList
示例 [5,7,9] 字符串数组列表,到索引 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: ["5","7","9"]
getWeeklyDataList: ["10","11","12"]
保证值=[]
org.json.JSONException: 类型为 java.lang.String 的 0 处的值 5 无法转换为 org.json.JSON.typeMismatch(JSON.java:100) 处的 JSONObject
【问题讨论】:
-
错误消息说您正在尝试将
"5"转换为JSONObject,这是不可能的。你有什么不清楚的地方? -
是的兄弟,我想将整个字符串 [5,7,9] 作为 INTEGER 存储在 listOfGuarantees ArrayList 索引 0 处
-
你看过我的评论了吗?
What is not clear for you? -
是的,我正面临将 [5,7,9] stringArrayList 存储为 IntegerArrayList 的问题
-
好的,你知道JsonObject是什么吗?你知道
"5"显然不是一个有效的JsonObject 吗?你为什么要尝试将"5"转换为 JsonObject 呢?你为什么不把 is 作为一个整数?
标签: android arrays json listview android-recyclerview