返回的json文件没有jsonArray,因此无法从Android Studio获取输出
我的项目使用Android 5.1,然后我尝试从服务器返回json文件,结果如下:返回的json文件没有jsonArray,因此无法从Android Studio获取输出,android,json,Android,Json,我的项目使用Android 5.1,然后我尝试从服务器返回json文件,结果如下: [ { "id": 3, "title": "asdiasjdaklsj", "description": "dfkldjskdhjsfkldjsfkl", "owner": "kyaaa", "start": "01/27/2016 12:00 AM", "end": "01/28/2016 12:0
[
{
"id": 3,
"title": "asdiasjdaklsj",
"description": "dfkldjskdhjsfkldjsfkl",
"owner": "kyaaa",
"start": "01/27/2016 12:00 AM",
"end": "01/28/2016 12:01 AM"
}
]
"employees":[
{"id":"John", "title":"Doe"},
{"id":"Anna", "title":"Smith"},
{"id":"Peter","title":"Jones"}
]
可用于android的结果如下:
[
{
"id": 3,
"title": "asdiasjdaklsj",
"description": "dfkldjskdhjsfkldjsfkl",
"owner": "kyaaa",
"start": "01/27/2016 12:00 AM",
"end": "01/28/2016 12:01 AM"
}
]
"employees":[
{"id":"John", "title":"Doe"},
{"id":"Anna", "title":"Smith"},
{"id":"Peter","title":"Jones"}
]
上面的Json是我使用的代码中的工作结果,但当我使用自己的Json时,它将返回错误,应用程序将停止工作。我想知道这有什么问题
package net.simplifiedcoding.volleysample;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by Belal on 9/22/2015.
*/
public class ParseJSON {
public static String[] ids;
public static String[] titles;
public static String[] descriptions;
public static String[] owners;
public static String[] starts;
public static String[] ends;
public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_OWNER = "owner";
public static final String KEY_START = "start";
public static final String KEY_END = "end";
private JSONArray users = null;
private String json;
public ParseJSON(String json){
this.json = json;
}
protected void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
ids = new String[users.length()];
titles = new String[users.length()];
descriptions = new String[users.length()];
owners = new String[users.length()];
starts = new String[users.length()];
ends = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
titles[i] = jo.getString(KEY_TITLE);
descriptions[i] = jo.getString(KEY_DESCRIPTION);
owners[i] = jo.getString(KEY_OWNER);
starts[i] = jo.getString(KEY_START);
ends[i] = jo.getString(KEY_END);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
package net.simplifiedcoding.volleysample;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
/**
*由Belal于2015年9月22日创建。
*/
公共类解析JSON{
公共静态字符串[]id;
公共静态字符串[]标题;
公共静态字符串[]描述;
公共静态字符串[]所有者;
公共静态字符串[]开始;
公共静态字符串[]结束;
公共静态最终字符串JSON_ARRAY=“result”;
公共静态最终字符串键\u ID=“ID”;
公共静态最终字符串键\u TITLE=“TITLE”;
公共静态最终字符串键\u DESCRIPTION=“DESCRIPTION”;
公共静态最终字符串键\u OWNER=“OWNER”;
公共静态最终字符串键\u START=“START”;
公共静态最终字符串键\u END=“END”;
私有JSONArray用户=null;
私有字符串json;
公共解析json(字符串json){
this.json=json;
}
受保护的void parseJSON(){
JSONObject JSONObject=null;
试一试{
jsonObject=新的jsonObject(json);
users=jsonObject.getJSONArray(JSON_数组);
ids=新字符串[users.length()];
titles=新字符串[users.length()];
descriptions=新字符串[users.length()];
所有者=新字符串[users.length()];
开始=新字符串[users.length()];
ends=新字符串[users.length()];
对于json[]中的(int i=0;i表示数组和{}表示对象。您应该先使用JsonArray,然后才能对对象进行迭代。您的服务器响应似乎是数组而不是对象,请尝试进行此更改,而不是将其强制转换为JSONObject,然后尝试从中获取JsonArray
JSONArray users = new JSONArray(json);
希望有帮助!您的Web服务正在返回一个JSONArray
。您为什么试图从中创建JSONObejct
?我已经编辑了我的问题,您可能会更了解它。@blackbelt我不明白这两件事是怎么回事connected@Blackbelt我的问题是,当我试图从我的服务器获取数据时,它无法下拉和删除在我的listview中使用。然后我想可能是我返回的结果没有满足要求。在我更改它之后,仍然会出现错误。你会得到什么错误?另外,你能告诉我什么是json
吗?我的意思是你能粘贴你正在使用的字符串json
?这可能有助于更好地调试吗问题。因为我已经在ParseJSON.java上添加了整个代码,而您正在手动传递一个字符串,还是一个服务器响应?我的意思是日志或该字符串的打印。所以这意味着,代码可以正常工作?但我返回的结果不能正常工作。但是,在laravel中,我应该做什么,然后可以返回w使用json数组?