[英]JSON Exception: java.lang.String cannot be converted to JSONObject
I'm having a JSON Exception being thrown and I don't know why it is being thrown. 我正在抛出JSON异常,但我不知道为什么要抛出它。 I've taken a look at almost all of the other questions that are just like mine, but I think mine is different. 我看了几乎所有其他问题,就像我的一样,但我认为我的问题有所不同。 So I am getting a JSONArray from a webpage, and the JSON it sends is valid (I checked it with a validator). 因此,我从网页上获取了一个JSONArray,并且它发送的JSON是有效的(我使用验证程序对其进行了检查)。 The exception is being thrown when I perform getJSONObject on the JSONArray. 当我在JSONArray上执行getJSONObject时,将引发异常。
Here's exactly what it says (personal information is starred out): 这就是它所说的(个人信息已加注星标):
org.json.JSONException: Value [{"name":"*****","profilePicture":"*****"}] at 0 of type java.lang.String cannot be converted to JSONObject
And here's my Java code: 这是我的Java代码:
protected Void doInBackground(JSONObject... params) {
JSONObject jsonObject = params[0];
ClientServerInterface clientServerInterface = new ClientServerInterface();
JSONArray attendanceDetails = clientServerInterface.postData("http://54.164.136.46/get_attendance.php", jsonObject);
Log.e("Attendance Details: ", attendanceDetails.toString());
nameAttendees = new String[attendanceDetails.length()];
pictureAttendees = new String[attendanceDetails.length()];
JSONObject jobj = null;
for(int i = 0; i < attendanceDetails.length(); i++)
{
try {
jobj = attendanceDetails.getJSONObject(i);
nameAttendees[i] = jobj.getString("name");
pictureAttendees[i] = jobj.getString("profilePicture");
} catch (JSONException e) {
e.printStackTrace();
}
The error is happening at the line where I go: 错误发生在我去的那一行:
jobj = attendanceDetails.getJSONObject(i);
Any help is greatly appreciated. 任何帮助是极大的赞赏。
Try this: 尝试这个:
for(int i = 0; i < attendanceDetails.length(); i++)
{
try {
JSONArray hello = new JSONArray(attendanceDetails.getJSONArray(i));
JSONObject jObject = new JSONObject(hello.get(i).toString());
nameAttendees[i] = jObject.getString("name");
pictureAttendees[i] = jObject.getString("profilePicture");
} catch (JSONException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.