【问题标题】:How to handle null response for json array in android如何在android中处理json数组的空响应
【发布时间】:2013-12-14 01:45:17
【问题描述】:

我收到了一个 json 响应

 {
    "tasks": null,
    "projects": null

  } 

如何判断数组是否有空值并处理?

谢谢:)

【问题讨论】:

  • 数组是什么意思?你的意思是响应为空或特定字段为空?
  • @SathishKumar:假设“tasks”和“projects”通常是数组,但在本例中它们都设置为null。
  • 特定字段。这里的tasks是json数组。没有数据时返回null。
  • 嗨,你能用根更新完整的字符串吗?这个数据来自数组或 Jsonobject

标签: android json


【解决方案1】:

试试这个,

jsonObject.isNull("field-name")

参考:JSON null handlingthis

【讨论】:

    【解决方案2】:
    Try This
    =============
    try {
            String url;
        JSONObject JsonData = new JSONObject("YOUR FULL JSONSTRING");
        JSONArray webData = JsonData.getJSONArray("YOUR ROOT NAME OF YOUR JSON RESPONSE");
    
        for(int i=0;i<webData.length();i++)
        {
            JSONObject TeampObj = webData.getJSONObject(i);
            if(TeampObj.getString("tasks").trim().length()>0)
            {
                //store data in arrylist or string array
    
            }
            if(TeampObj.getString("projects").trim().length()>0)
            {
                //store data in arrylist or string array
            }
        }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    【讨论】:

    • 把你的 Fuul 字符串给我,我把完整解析发给你
    【解决方案3】:

    试试..

     public boolean isNull (String name)
    

    在 API 级别 1 中添加 如果此对象没有 name 映射或者它有一个值为 NULL 的映射,则返回 true。

    【讨论】:

      【解决方案4】:

      使用 try ... catch 处理异常:

      Try{
      
         // parsing data
      
      }catch (NullPointerException e) {
          e.printStackTrace();
      } catch (Exception e) {
          e.printStackTrace();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多