android - 0 处的值无法转换为 JSON 对象
问题描述
我正在从 读取字符串数据SharedPreference
。我的字符串的格式是
[{
"name": "sdf",
"phone": "2356235623",
"no_people": "2"
}]
我想阅读此中JSONobjects
的所有内容JSONArray
。所以我将此字符串转换为 JSON 数组
String tempData = addqueuetemp.getString("tempdata", "");
JSONArray cacheTemp=new JSONArray(tempData);
现在我将这个 JSON 数组解析为
JSONArray cacheTemp=new JSONArray(tempQueue);
for(int i=0;i<cacheTempQueue.length();i++){
JSONObject tempObject=cacheTemp.getJSONObject(i);
CurrentStatusEntry tempEntry=new CurrentStatusEntry();
tempEntry.setName(tempObject.getString("name");
current.add(tempEntry);
adapter = new QueueAdapter(current, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
}
但我收到错误
org.json.JSONException: Value {"name":"sdf","phone":"2356235623","no_people":"2"} at 0 of type java.lang.String cannot be converted to JSONObject
如何解决这个问题?
解决方案
用这个
JSONArray cacheTemp=new JSONArray(tempQueue);
for(int i=0;i<cacheTemp.length();i++){
JSONObject tempObject=cacheTemp.getJSONObject(i);
CurrentStatusEntry tempEntry=new CurrentStatusEntry();
tempEntry.setName(tempObject.getString("name");
current.add(tempEntry);
adapter = new QueueAdapter(current, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
}
推荐阅读
- c# - C#如何查找和替换字符串中的特定文本?
- perl - 从两个不同长度数组的比较中获取索引
- c# - 触发数据库事件时成员变量未更新
- netsuite - suitescript 2.0:当我使用 removeSelectOption 时,抛出“TypeError 无法读取 null 的属性“removeSelectOption””错误
- ios - 如何使用警报来确认或取消视图转换?
- switching - 第 2 层跟踪路由的范围是什么,相同的子网或外部也是可能的
- c - 创建 C DLL 并在 Golang 中使用它?
- database-design - 设备及零部件监控系统
- google-analytics - 从 analytics.js 迁移到 gtag.js
- java - 我需要有关创建包含 Post 请求方法链接的 selenium 脚本的帮助