zoukankan      html  css  js  c++  java
  • Java 中判断 JSONObject 对应的 VALUE 为空

    目前发现有两种包.两种不一样的json包.
    第一种情况是: json包是json-lib包是net.sf.json

    怎样判断JSONObject返回的是字符串null还是null值.

    研究源码发现.JSONObject里有一段代码是当遇到json串中是null的时候.返回JSONNUll.
    所以区分null时这样:
    JSONObject jo = JSONObject.fromObject("{a:null,b:"null"}");
    Object o = jo.get("a");
    if(o instanceof JSONNull){
    System.out.println("Is empty null");
    }else{
    System.out.println("is String null");
    }
    o = jo.get("b");
    if(o instanceof JSONNull){
    System.out.println("Is empty null");
    }else{
    System.out.println("is String null");
    }
    输入的结果为
     
    第二种情况是: org.json的包
     
    JSONObject jo = new JSONObject(("{"a":"null","b":null}"));
    if(jo.get("a") instanceof String){
    System.out.println("a is String null");
    }else{
    System.out.println("a is empty null");
    }
    if(jo.get("b") instanceof String){
    System.out.println("a is String null");
    }else{
    System.out.println("a is empty null");
    }
    System.out.println(jo.get("b").getClass());
    这时候发现.返回的null是JSONObject.NUll
    两种包不一样的返回NUll值
  • 相关阅读:
    redis集群redis-cloud搭建
    Linux下搭建redis服务器
    mybatis主键返回
    分布式文件系统FastDFS
    mapper映射文件不发布
    dubbo发布和引用服务
    PageHelper分页插件
    F. Cards and Joy
    E. Paint the Tree 树形dp
    D. Sequence Sorting dp
  • 原文地址:https://www.cnblogs.com/jpfss/p/7249146.html
Copyright © 2011-2022 走看看