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值
  • 相关阅读:
    uva400 Unix ls
    cf641 div2 abcd
    cf619 div2 abcd
    cf620 div2 abcde
    atc160
    cf638 div2 abcd
    CodeCraft-20(Div. 2 abcd
    cf Round 621 abcd
    luogu1941 飞扬的小鸟
    UVA1601 The Morning afther Halloween
  • 原文地址:https://www.cnblogs.com/jpfss/p/7249146.html
Copyright © 2011-2022 走看看