android - 0 处的值无法转换为 JSON 对象

标签 android arrays sharedpreferences android-json

我正在从 SharedPreference 读取字符串数据。我的字符串格式是

[{
    "name": "sdf",
    "phone": "2356235623",
    "no_people": "2"
}]

我想读取此 JSONArray 中存在的所有 JSONobjects。所以我将这个字符串转换为 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);
    }  

关于android - 0 处的值无法转换为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53042694/

相关文章:

android - 如何将此 JSON 对象转换为 String 数组?

javascript - a = [未定义] 和 a = new Array(1) 之间的区别

javascript - 非等于数组同时更改值javascript

android SharedPreferences putStringSet 顺序/排序

android - 通过服务使用存储的 MAC 地址连接到蓝牙设备

android - 如何像在 Swift 中一样从 Kotlin 中的函数返回多个值?

android - 编辑文本 : Disable Paste/Replace menu pop-up on Text Selection Handler click event

java - 如何在 LibGDX 中使用 DIP/DP

java - java中如何将2Darray转换为2D ArrayList?

android - 如何将切换按钮状态与前台服务同步?