[英]JSON Data Parsing Error the response from server becomes null: Android
我正在嘗試從服務器獲取數據,然后將其顯示到應用程序中。
我有JSON數據,
{"COLUMNS":["TIMEID","BRANCHID","COMPANYID","MON_O","TUE_O","WED_O","THU_O","FRI_O","SAT_O","SUN_O","MON_C","TUE_C","WED_C","THU_C","FRI_C","SAT_C","SUN_C","CREATDATE"],"DATA":[[195,4,4,"09:00","09:00","09:00","09:00","09:00","Closed","Closed","16:30","16:30","16:30","16:30","16:30","Closed","Closed","May, 16 2017 08:16:12"]]}
我有JAVA課程,
public static final String MON_O = "MON_O";
public static final String TUE_O = "TUE_O";
public static final String WED_O = "WED_O";
public static final String THU_O = "THU_O";
public static final String FRI_O = "FRI_O";
public static final String SAT_O = "SAT_O";
public static final String SUN_O = "SUN_O";
public static final String MON_C = "MON_C";
public static final String TUE_C = "TUE_C";
public static final String WED_C = "WED_C";
public static final String THU_C = "THU_C";
public static final String FRI_C = "FRI_C";
public static final String SAT_C = "SAT_C";
public static final String SUN_C = "SUN_C";
public static final String JSON_ARRAY = "COLUMNS";
private void getData() {
String url = context.getString(site_url)+"branch_time.cfc?method=branchtime&branchid=" +dBranchID;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
String name = "";
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("TAG", jsonObject.toString());
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
Log.d("TAG", result.toString());
JSONObject companyData = result.getJSONObject(0);
name = companyData.getString(MON_O);
Log.e(name,"datadtadattadtatadtat");
} catch (JSONException e) {
e.printStackTrace();
}
timeStatus.setText(name);
}
當我嘗試記錄服務器的響應時,我可以看到只有DATA值為null的COLUMNS值存在。 另外,我收到如下錯誤,
System.err: org.json.JSONException: Value TIMEID at 0 of type java.lang.String cannot be converted to JSONObject
為什么我要從服務器獲取DATA值為null以及如何解決該錯誤? 我已經引用了以下鏈接: org.json.JSONException:類型java.lang.String的值不能轉換為JSONArray
解析數據org.json.JSONException時出錯:類型java.lang.String的值<br不能轉換為JSONObject
但是,這些對我來說似乎無濟於事。 有人可以幫忙嗎?
JSONParser jsonParser = new JSONParser();
jsonObject = jsonParser.getJSONFromUrl(url);
try {
user1 = jsonObject.getJSONArray("COLUMNS");
for (int i = 0; i < user1.length(); i++) {
String value = (String) user1.get(i);
getList.add(value.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
我觀察到您的json響應 ,我認為無論您在列json數組中有什么,都沒有字符串,這就是為什么您會出錯的原因,
在您的響應中,沒有鍵值對,因此很容易獲得String。
所以你應該用getString()而不是getJsonObject()
private void showJSON(String response) {
String name = "";
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("TAG", jsonObject.toString());
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
Log.d("TAG", result.toString());
name = result.getString(index);
Log.e(name,"datadtadattadtatadtat");
} catch (JSONException e) {
e.printStackTrace();
}
index基本上是json數組索引,您可以將其設置為0或其他值,並根據需要進行循環 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.