Значение 0 не может быть преобразовано в объект JSON - PullRequest
0 голосов
/ 29 октября 2018

Я читаю строковые данные из 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

Как решить эту проблему?

Ответы [ 3 ]

0 голосов
/ 29 октября 2018

Используйте это

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);
    }  
0 голосов
/ 29 октября 2018

Используйте cacheTemp JSONArray для цикла for вместо cacheTempQueue и поместите ваш код setAdateper во внешний цикл.

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);
0 голосов
/ 29 октября 2018

попробуйте

JSONObject obj1 = new JSONObject(result.toString());
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj1);
JSONObject dataObj = new JSONObject();
dataObj.put("Data", jsonArray);
JSONArray Jarray = obj.getJSONArray("value");
...