java 中判斷JSONObject為空白

來源:互聯網
上載者:User

標籤:字串   hup   情況   ceo   src   else   aaa   empty   bsp   

目前發現有兩種包.兩種不一樣的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值

java 中判斷JSONObject為空白

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.