zoukankan      html  css  js  c++  java
  • Android填坑系列:Android JSONObject 中对key-value为null的特殊处理

    在与服务端通过JSON格式进行交互过程中,不同版本的JSON库在对于key-value为null情况上的处理不同。

    Android自带的org.json对key-value都要求不能为null,对于必传的字段需要留意一下,尤其是留意value是否可能出现null的情形。否则导致服务端解析出现问题。

    此坑已被踩中,留下小记。下面直接看一下相应位置源码:

     1 public class JSONObject {
     2 
     3     ......
     4 
     5     /**
     6      * Maps {@code name} to {@code value}, clobbering any existing name/value
     7      * mapping with the same name. If the value is {@code null}, any existing
     8      * mapping for {@code name} is removed.
     9      *
    10      * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
    11      *     Integer, Long, Double, {@link #NULL}, or {@code null}. May not be
    12      *     {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
    13      *     infinities}.
    14      * @return this object.
    15      */
    16     public JSONObject put(String name, Object value) throws JSONException {
    17         if (value == null) {
    18             nameValuePairs.remove(name);
    19             return this;
    20         }
    21         if (value instanceof Number) {
    22             // deviate from the original by checking all Numbers, not just floats & doubles
    23             JSON.checkDouble(((Number) value).doubleValue());
    24         }
    25         nameValuePairs.put(checkName(name), value);
    26         return this;
    27     }
    28     
    29     
    30     String checkName(String name) throws JSONException {
    31         if (name == null) {
    32             throw new JSONException("Names must be non-null");
    33         }
    34         return name;
    35     }
    36     
    37     
    38     ......
    39     
    40 }
  • 相关阅读:
    Mysql性能优化
    PHP IF判断简写
    PHP与MYSQL事务处理
    js获取select标签选中的值
    oralce 的安装以及plsql的配置的html连接
    mysql 中启动服务的命令 、登录命令、退出命令 mysql 的常用命令
    oracle 中 某个字段的长度不够的sql 语句
    随机获得id的方法
    java中解析excel 批量插入数据库
    java 调用存储过程
  • 原文地址:https://www.cnblogs.com/lwbqqyumidi/p/5491074.html
Copyright © 2011-2022 走看看