我正在从SharedPreference
读取String数据。我的String的格式为
[{
"name": "sdf",
"phone": "2356235623",
"no_people": "2"
}]
我想阅读此JSONobjects
中存在的所有JSONArray
。所以我将这个字符串转换为
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
如何解决这个问题?
答案 0 :(得分:1)
尝试
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");
答案 1 :(得分:1)
使用此
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);
}
答案 2 :(得分:0)
在循环中使用import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class StackOverFlow {
public static void main(String[] args) {
try {
String sText = getFileText("C:/file.txt");
System.out.println("Text is: " + sText);
} catch (FileNotFoundException e) {
System.out.println("File not Found");
} catch (IOException e) {
System.out.println("@Error while reading text: " + e.getMessage());
}
}
private static String getFileText(String filePath) throws FileNotFoundException, IOException {
File file = new File(filePath);
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
reader.close();
}finally {
reader.close();
}
return new String(stringBuilder);
}
}
JSONArray代替shell_exec()
,然后将setAdateper代码放入外部循环。
shell_exec("/usr/bin/nohup curl -o /dev/null --data \"$data\" $url >/dev/null 2>&1 &");