【问题标题】:JSON Data Parsing Error the response from server becomes null: AndroidJSON数据解析错误来自服务器的响应变为空:Android
【发布时间】:2017-11-18 16:40:29
【问题描述】:

我正在尝试从服务器获取数据,然后将其显示到应用程序中。

我有 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);

}

当我尝试记录来自服务器的响应时,我可以看到只有 COLUMNS 值在那里,因为 DATA 为空。 另外,我收到如下错误,

System.err: org.json.JSONException: Value TIMEID at 0 of type java.lang.String cannot be converted to JSONObject

为什么我会从服务器获取 DATA 值作为空值,我应该如何修复错误? 我已经参考了链接:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

但是,这些似乎对我没有帮助。请问有人可以帮忙吗?

【问题讨论】:

    标签: android arrays json android-json


    【解决方案1】:

    试试这个.....

    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();
                }
    

    【讨论】:

      【解决方案2】:

      我观察到你的 json 响应,我认为无论你在列 json 数组中拥有什么,都没有你得到的字符串,这就是你得到错误的原因,

      在您的回复中没有键值对,因此很容易获得字符串。

      所以你应该 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 或其他东西,如果你愿意,可以去 for 循环

      【讨论】:

      • 是的,它确实有效。非常感谢。但是我仍然从服务器响应中获取数据为空。你能建议什么会导致这个问题吗?
      • 是什么意思?您的数据是否为空?
      • 服务器中的数据有 COLUMNS 和 DATA。但是,我从服务器得到的响应只有 COLUMNS 的值,而 DATA 为空。
      猜你喜欢
      • 2013-02-13
      • 2011-01-21
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      相关资源
      最近更新 更多