【问题标题】:Json Array giving ExceptionJson 数组给出异常
【发布时间】:2016-07-25 08:49:54
【问题描述】:

以下是我的 Json 响应 -

CallWMDefaultWithRef1Response{CallWMDefaultWithRef1Result=[{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":,"USER_ID":30,"ROLE_ID":4,"USER_NAME":""}]; }

下面是我的代码 -

if (resultlog!= null) {

  try {
    JSONArray resultarray = new JSONArray(resultlog);
    for (int i = 0; i < resultarray.length(); i++) {
      JSONObject c = resultarray.getJSONObject(i);
      roleName = c.getString("ROLE_NAME");
      userMobile = c.getString("USER_MOBILE");
      imeiNumber = c.getString("IMEI_NUMBER1");
      userId = c.getString("USER_ID");
      roleid = c.getString("ROLE_ID");
      userNmae = c.getString("USER_NAME");
    }
  } catch (JSONException e) {
    e.printStackTrace();
  }
}

以下是我的错误

org.json.JSONException: 值 CallWMDefaultWithRef1Response 类型 java.lang.String 无法转换为 JSONArray

【问题讨论】:

  • 首先检查你的 json 格式
  • “IMEI_NUMBER1”:缺少值
  • 您能否在致电new JSONArray(...) 之前登录resultlog - 您似乎也没有为属性IMEI_NUMBER1 指定值。
  • 所有键和值都正确出现。我在我的问题中隐藏了峨眉山。否则所有的价值都来了……
  • 这一行是否出现错误“JSONArray resultarray = new JSONArray(resultlog);”我认为您在这里不需要 JSONArray,它应该是“JSONObject”这样的 JSONObject result = new JSONObject(resultlog);

标签: android arrays json soap


【解决方案1】:

试试我的代码

try{
   JSONObject result=resultlog.getJSONObject("CallWMDefaultWithRef1Response");
   JSONArray resultarray=result.getJSONArray("CallWMDefaultWithRef1Result");
      for(int i=0;i<resultarray.length();i++){
         JSONObject c=resultarray.getJSONObject(i);
         roleName=c.getString("ROLE_NAME");
         userMobile=c.getString("USER_MOBILE");
         imeiNumber=c.getString("IMEI_NUMBER1");
         userId=c.getString("USER_ID");
         roleid=c.getString("ROLE_ID");
         userNmae=c.getString("USER_NAME");
      }
 } catch(JSONException e) {
      e.printStackTrace();
 }

【讨论】:

    【解决方案2】:

    您的结果日志字符串似乎是

    CallWMDefaultWithRef1Response{CallWMDefaultWithRef1Result=[{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":,"USER_ID":30,"ROLE_ID":4,"USER_NAME" :""}]; }

    它包含一个字符串 (CallWMDefaultWithRef1Response) 以及 JSON 内容。您应该只将 JSON 内容传递给新的 JSONArray() 构造函数。

    要从结果日志中提取 JSON 数组,请尝试

    resultlog = resultlog.substring(jsonStr.indexOf("["), jsonStr.lastIndexOf("]")+1);
    

    所以结果日志将是

    [{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":,"USER_ID":30,"ROLE_ID":4,"USER_NAME":""}]

    注意:此处“IMEI_NUMBER1”后缺少文字值 这将引发 JSONException。您应该在服务器中将其更正为 在 IMEI_NUMBER1 之后包含一个空字符串。

    【讨论】:

      【解决方案3】:

      您的错误是使用整个字符串作为JSONArray

      从日志中删除 CallWMDefaultWithRef1Response。然后从剩下的内容中创建一个新的JSONObject,然后得到JSONArray CallWMDefaultWithRef1Result 从对象中获取并循环从array 中获取对象。

      【讨论】:

        【解决方案4】:

        resultLog 下面是你的实际 json 结果。

            String roleName = "", userMobile = "", imeiNumber = "", userNmae = "";
            int userId = 0, roleid = 0;
        

        (或)

        如果您的流程以 CallWMDefaultWithRef1Response 作为 jsonObject 开始,那么您应该在 try 块中添加以下代码行。

            String resultlog= "{CallWMDefaultWithRef1Response:{CallWMDefaultWithRef1Result:{"ROLE_NAME":"","USER_MOBILE":"","IMEI_NUMBER1":"","USER_ID":30,"ROLE_ID":4,"USER_NAME":""}}}
            JSONObject resultJsonObject = null;
            try {
                JSONObject obj1 = new JSONObject(resultlog);
                JSONObject obj2 = obj1.has("CallWMDefaultWithRef1Response") ? obj1.getJSONObject("CallWMDefaultWithRef1Response") : null;
                resultJsonObject = obj2.has("CallWMDefaultWithRef1Result") ? obj2.getJSONObject("CallWMDefaultWithRef1Result") : null;
                roleName = resultJsonObject.has("ROLE_NAME") ? resultJsonObject.getString("ROLE_NAME") : "";           
                userMobile = resultJsonObject.has("USER_MOBILE") ? resultJsonObject.getString("USER_MOBILE") : "";
                imeiNumber = resultJsonObject.has("IMEI_NUMBER1") ? resultJsonObject.getString("IMEI_NUMBER1") : "";
                userId = resultJsonObject.has("USER_ID") ? resultJsonObject.getInt("USER_ID") : "";            
                roleid = resultJsonObject.has("ROLE_ID") ? resultJsonObject.getInt("ROLE_ID") : "";            
                userNmae = resultJsonObject.has("USER_NAME") ? resultJsonObject.getString("USER_NAME") : "";           
        
           } catch (JSONException e) {
               e.printStackTrace();
           }
        

        否则在下面使用:

             String resultlog = "{\"ROLE_NAME\":\"\",\"USER_MOBILE\":\"\",\"IMEI_NUMBER1\":\"\",\"USER_ID\":30,\"ROLE_ID\":4,\"USER_NAME\":\"\"}";
            try {
                JSONObject resultJsonObject = new JSONObject(resultlog);          
                    roleName = resultJsonObject.has("ROLE_NAME") ? resultJsonObject.getString("ROLE_NAME") : "";           
                    userMobile = resultJsonObject.has("USER_MOBILE") ? resultJsonObject.getString("USER_MOBILE") : "";
                    imeiNumber = resultJsonObject.has("IMEI_NUMBER1") ? resultJsonObject.getString("IMEI_NUMBER1") : "";
                    userId = resultJsonObject.has("USER_ID") ? resultJsonObject.getInt("USER_ID") : "";            
                    roleid = resultJsonObject.has("ROLE_ID") ? resultJsonObject.getInt("ROLE_ID") : "";            
                    userNmae = resultJsonObject.has("USER_NAME") ? resultJsonObject.getString("USER_NAME") : "";           
        
            } catch (JSONException e) {
                e.printStackTrace();
            }
        

        【讨论】:

          【解决方案5】:

          我已经通过非常简单的步骤解决了这个问题-

          try {
                JSONArray resultarray = new JSONArray(resultlog.getProperty(0));
                for (int i = 0; i < resultarray.length(); i++) {
                          JSONObject c = resultarray.getJSONObject(i);
                          roleName = c.getString("ROLE_NAME");
                          userMobile = c.getString("USER_MOBILE");
                          imeiNumber = c.getString("IMEI_NUMBER1");
                          userId = c.getString("USER_ID");
                          roleid = c.getString("ROLE_ID");
                          userNmae = c.getString("USER_NAME");
                  }
                              } catch (JSONException e) {
                                  e.printStackTrace();
                              }
          

          请喜欢我的回答以帮助他人。

          快乐编码..

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-06
            • 2021-04-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多