【问题标题】:Cannot Display Json Array values from JSONData无法显示来自 JSONData 的 Json 数组值
【发布时间】:2017-01-18 07:39:59
【问题描述】:

我正在尝试显示 JSONData 中的 JsonArray 值

JSON DATA

{"error":{"group_name":["组名已被占用。"]}}



这是我的代码

 httppost.setEntity(new UrlEncodedFormEntity(params));
 HttpResponse response = httpclient.execute(httppost);
 String responseStr = EntityUtils.toString(response.getEntity());
 JSONObject json = new JSONObject(responseStr);
 Log.d("Checking Login", responseStr);
 JSONArray jsonArray = json.getJSONArray("group_name");
    for(int i = 0; i<jsonArray.length();i++){
       JSONObject jsonObject = jsonArray.getJSONObject(i);
    }

我收到类似这样的异常
org.json.JSONException: No value for group_name

【问题讨论】:

    标签: java android arrays json http


    【解决方案1】:

    error 嵌套在 JSONObject 中,其中包含您的 JSONArray

        JSONObject json = new JSONObject(responseStr);
        JSONObject json1 = json.getJSONObject("error");
        //                                  ^^^^^ fetch nested JSON
         JSONArray jsonArray = json1.getJSONArray("group_name");
            for(int i = 0; i<jsonArray.length();i++){
               // JSONObject jsonObject = jsonArray.getJSONObject(i); error
               // jsonArray has no JSONOBJECT but it has String
            }
    

    注意:您的group_name JSONArray 没有jsonObjects,而是根据您展示的示例只有一个String,因此jsonArray.getJSONObject(i) 将导致异常。


    {"error":{"group_name":["The group name has already been taken."]}}
                            |---------------String-----------------|
                           |---------------JSONOArray---------------|
             |-----------------Nested JSONOBJECT---------------------|
    |------------------------JSONOBJECT-------------------------------|
    

    所以你只有String 在你JSONArray 所以使用optString 来获取你的String

        JSONObject json = new JSONObject(responseStr);
        JSONObject json1 = json.getJSONObject("error");
        //                                  ^^^^^ fetch nested JSON
         JSONArray jsonArray = json1.getJSONArray("group_name");
            for(int i = 0; i<jsonArray.length();i++){
               String str = jsonArray.optString(i);
            }
    

    【讨论】:

    • 我得到这个 - org.json.JSONException: java.lang.String 类型的值错误无法转换为 JSONObject
    • 你能推荐我正确的 Json 数据吗? Group_name 值应该是数组。
    • @DevTamil 您必须发送包含jsonobjectsGroup_name 或使用optString 来解析您当前的JSON 响应
    • 我正在调试它。当它到达 JSONARRAY 行时,它会在开始循环之前跳转到 catch。
    • 工作得很好,谢谢@Pavneet Singh
    【解决方案2】:

    "error" 会像这样解析:

    JSONObject json = new JSONObject(responseStr);
    JSONObject jsonError = json.getJSONObject("error");
    JSONArray jsonArray = jsonError.getJSONArray("group_name");
    for(int i = 0; i<jsonArray.length();i++){
          JSONObject jsonObject = jsonArray.getJSONObject(i);
     }
    

    【讨论】:

      【解决方案3】:

      这样就可以得到字符串

      for(int i = 0; i<jsonArray.length();i++){
             String groupName = jsonArray.optString(i);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        • 2017-06-01
        • 2018-08-28
        • 2020-02-08
        • 2018-09-21
        相关资源
        最近更新 更多