org.json.JSONException: 0 没有值
org.json.JSONException: No value for 0
我收到 API 的回复。
但是一旦我到达 return 命令,我就会收到以下错误:
W/System.err: org.json.JSONException: No value for 0
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getInt(JSONObject.java:478)
W/System.err: at bobbydejong.expandabletest.OrderOverzicht.prepareOrderList(OrderOverzicht.java:65)
W/System.err: at bobbydejong.expandabletest.OrderOverzicht.onSuccess(OrderOverzicht.java:37)
W/System.err: at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:113)
W/System.err: at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:21)
W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:660)
W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
它发生在这段代码上:
public class GetItemsSwagger extends AsyncTask<String, Void, String> {
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
String result2;
private OnEventListener<String> mCallBack;
public Exception mException;
private Context context;
HttpURLConnection conn = null;
OutputStream os = null;
InputStream is = null;
String toastMsg = "Failed to get API response.";
String apiKey;
public GetItemsSwagger(Context context, OnEventListener callback) {
this.context = context;
this.mCallBack = callback;
}
@Override
protected String doInBackground(String... strings) {
try {
apiKey = strings[1];
URL url = new URL(strings[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
conn.setUseCaches(false);
conn.setRequestProperty("apikey", apiKey);
int status = conn.getResponseCode();
if(status >= 400)
is = conn.getErrorStream();
else
is = conn.getInputStream();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
} else {
toastMsg = "";
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(is)));
String output;
while ((output = br.readLine()) != null) {
toastMsg += output;
}
conn.disconnect();
result2= "";
return toastMsg;
编辑:
[{"orderGroupID":"1","name":"Patat","orderStatusID":"8","orderTo":"2017-02-24 11:15:00","created":"2017-02-24 10:04:21"},{"orderGroupID":"2","name":"Patat","orderStatusID":"8","orderTo":"2017-03-03 11:00:00","created":"2017-03-03 10:06:53"},{"orderGroupID":"3","name":"Patat","orderStatusID":"8","orderTo":"2017-03-10 11:10:00","created":"2017-03-10 10:27:32"},{"orderGroupID":"4","name":"Patat","orderStatusID":"8","orderTo":"2017-03-17 11:00:00","created":"2017-03-17 08:27:48"},{"orderGroupID":"5","name":"Patat","orderStatusID":"8","orderTo":"2017-03-24 11:00:00","created":"2017-03-24 08:58:08"},{"orderGroupID":"6","name":"Patat","orderStatusID":"8","orderTo":"2017-03-31 11:00:00","created":"2017-03-31 09:26:01"},{"orderGroupID":"7","name":"Patat","orderStatusID":"8","orderTo":"2017-04-07 11:00:00","created":"2017-04-07 10:02:52"},{"orderGroupID":"8","name":"Patat","orderStatusID":"8","orderTo":"2017-04-14 11:00:00","created":"2017-04-14 09:40:43"},{"orderGroupID":"9","name":"Patat","orderStatusID":"8","orderTo":"2017-04-21 11:00:00","created":"2017-04-21 10:01:31"},{"orderGroupID":"11","name":"Patat","orderStatusID":"8","orderTo":"2017-04-28 11:00:00","created":"2017-04-28 10:12:45"},{"orderGroupID":"12","name":"Patat","orderStatusID":"8","orderTo":"2017-05-05 11:00:00","created":"2017-05-05 09:39:56"},{"orderGroupID":"13","name":"Patat","orderStatusID":"8","orderTo":"2017-05-12 11:10:00","created":"2017-05-12 09:52:26"},{"orderGroupID":"14","name":"Patat","orderStatusID":"8","orderTo":"2017-05-19 11:00:00","created":"2017-05-19 09:58:37"}]
这是 JSON 回复
try {
JSONArray jObjArray = new JSONArray(orderResult);
int length = jObjArray.length();
final ArrayList<String> orderList = new ArrayList<String>();
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<>();
JSONObject jObj = jObjArray.getJSONObject(i);
map.put("orderGroupID", jObj.getInt("orderGroupID"));
map.put("0", jObj.getInt("0"));
map.put("name", jObj.getString("name"));
map.put("orderStatusID", jObj.getInt("orderStatusID"));
map.put("2", jObj.getInt("2"));
map.put("orderTo", jObj.getString("orderTo"));
map.put("3", jObj.getString("3"));
map.put("created", jObj.getString("created"));
String orderTo = jObj.getString("orderTo");
String orderGroupID = jObj.getString("orderGroupID");
orderList.add(orderTo+" "+orderGroupID);
这是我操作代码的地方(不同class)
我期待您的回复,我一直试图在其他帖子上找到这个问题,但似乎这些解决方案对我来说并不奏效,而且问题也不完全相同。如果我错了请纠正我,但我是初学者!
首先,您的 JSON 响应中的 "orderGroupID" 值是一个 String,您正在尝试通过 jObj.getInt("orderGroupID"));
访问它。您应该使用 jObj.getString("orderGroupID"));
访问它
其次,在您的 JSON 响应中没有任何值为“0”的键,因此解析将抛出异常。
由于您的 orderGroupID
值为 string
,只需使用 jObj.getString("orderGroupID")
而不是 jObj.getInt("orderGroupID")
。
试试这个:
try {
JSONArray jObjArray = new JSONArray(orderResult);
int length = jObjArray.length();
final ArrayList<String> orderList = new ArrayList<String>();
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<>();
JSONObject jObj = jObjArray.getJSONObject(i);
map.put("orderGroupID", jObj.getString("orderGroupID"));
map.put("name", jObj.getString("name"));
map.put("orderStatusID", jObj.getString("orderStatusID"));
map.put("orderTo", jObj.getString("orderTo"));
map.put("created", jObj.getString("created"));
..........
.................
}
我收到 API 的回复。 但是一旦我到达 return 命令,我就会收到以下错误:
W/System.err: org.json.JSONException: No value for 0
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at org.json.JSONObject.getInt(JSONObject.java:478)
W/System.err: at bobbydejong.expandabletest.OrderOverzicht.prepareOrderList(OrderOverzicht.java:65)
W/System.err: at bobbydejong.expandabletest.OrderOverzicht.onSuccess(OrderOverzicht.java:37)
W/System.err: at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:113)
W/System.err: at bobbydejong.expandabletest.GetOrderSwagger.onPostExecute(GetOrderSwagger.java:21)
W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:660)
W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
它发生在这段代码上:
public class GetItemsSwagger extends AsyncTask<String, Void, String> {
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
String result2;
private OnEventListener<String> mCallBack;
public Exception mException;
private Context context;
HttpURLConnection conn = null;
OutputStream os = null;
InputStream is = null;
String toastMsg = "Failed to get API response.";
String apiKey;
public GetItemsSwagger(Context context, OnEventListener callback) {
this.context = context;
this.mCallBack = callback;
}
@Override
protected String doInBackground(String... strings) {
try {
apiKey = strings[1];
URL url = new URL(strings[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("GET");
conn.setUseCaches(false);
conn.setRequestProperty("apikey", apiKey);
int status = conn.getResponseCode();
if(status >= 400)
is = conn.getErrorStream();
else
is = conn.getInputStream();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
} else {
toastMsg = "";
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(is)));
String output;
while ((output = br.readLine()) != null) {
toastMsg += output;
}
conn.disconnect();
result2= "";
return toastMsg;
编辑:
[{"orderGroupID":"1","name":"Patat","orderStatusID":"8","orderTo":"2017-02-24 11:15:00","created":"2017-02-24 10:04:21"},{"orderGroupID":"2","name":"Patat","orderStatusID":"8","orderTo":"2017-03-03 11:00:00","created":"2017-03-03 10:06:53"},{"orderGroupID":"3","name":"Patat","orderStatusID":"8","orderTo":"2017-03-10 11:10:00","created":"2017-03-10 10:27:32"},{"orderGroupID":"4","name":"Patat","orderStatusID":"8","orderTo":"2017-03-17 11:00:00","created":"2017-03-17 08:27:48"},{"orderGroupID":"5","name":"Patat","orderStatusID":"8","orderTo":"2017-03-24 11:00:00","created":"2017-03-24 08:58:08"},{"orderGroupID":"6","name":"Patat","orderStatusID":"8","orderTo":"2017-03-31 11:00:00","created":"2017-03-31 09:26:01"},{"orderGroupID":"7","name":"Patat","orderStatusID":"8","orderTo":"2017-04-07 11:00:00","created":"2017-04-07 10:02:52"},{"orderGroupID":"8","name":"Patat","orderStatusID":"8","orderTo":"2017-04-14 11:00:00","created":"2017-04-14 09:40:43"},{"orderGroupID":"9","name":"Patat","orderStatusID":"8","orderTo":"2017-04-21 11:00:00","created":"2017-04-21 10:01:31"},{"orderGroupID":"11","name":"Patat","orderStatusID":"8","orderTo":"2017-04-28 11:00:00","created":"2017-04-28 10:12:45"},{"orderGroupID":"12","name":"Patat","orderStatusID":"8","orderTo":"2017-05-05 11:00:00","created":"2017-05-05 09:39:56"},{"orderGroupID":"13","name":"Patat","orderStatusID":"8","orderTo":"2017-05-12 11:10:00","created":"2017-05-12 09:52:26"},{"orderGroupID":"14","name":"Patat","orderStatusID":"8","orderTo":"2017-05-19 11:00:00","created":"2017-05-19 09:58:37"}]
这是 JSON 回复
try {
JSONArray jObjArray = new JSONArray(orderResult);
int length = jObjArray.length();
final ArrayList<String> orderList = new ArrayList<String>();
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<>();
JSONObject jObj = jObjArray.getJSONObject(i);
map.put("orderGroupID", jObj.getInt("orderGroupID"));
map.put("0", jObj.getInt("0"));
map.put("name", jObj.getString("name"));
map.put("orderStatusID", jObj.getInt("orderStatusID"));
map.put("2", jObj.getInt("2"));
map.put("orderTo", jObj.getString("orderTo"));
map.put("3", jObj.getString("3"));
map.put("created", jObj.getString("created"));
String orderTo = jObj.getString("orderTo");
String orderGroupID = jObj.getString("orderGroupID");
orderList.add(orderTo+" "+orderGroupID);
这是我操作代码的地方(不同class)
我期待您的回复,我一直试图在其他帖子上找到这个问题,但似乎这些解决方案对我来说并不奏效,而且问题也不完全相同。如果我错了请纠正我,但我是初学者!
首先,您的 JSON 响应中的 "orderGroupID" 值是一个 String,您正在尝试通过 jObj.getInt("orderGroupID"));
访问它。您应该使用 jObj.getString("orderGroupID"));
其次,在您的 JSON 响应中没有任何值为“0”的键,因此解析将抛出异常。
由于您的 orderGroupID
值为 string
,只需使用 jObj.getString("orderGroupID")
而不是 jObj.getInt("orderGroupID")
。
试试这个:
try {
JSONArray jObjArray = new JSONArray(orderResult);
int length = jObjArray.length();
final ArrayList<String> orderList = new ArrayList<String>();
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<>();
JSONObject jObj = jObjArray.getJSONObject(i);
map.put("orderGroupID", jObj.getString("orderGroupID"));
map.put("name", jObj.getString("name"));
map.put("orderStatusID", jObj.getString("orderStatusID"));
map.put("orderTo", jObj.getString("orderTo"));
map.put("created", jObj.getString("created"));
..........
.................
}