Android的JSON和空值 [英] Android Json and null values

查看:233
本文介绍了Android的JSON和空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能侦测到一个JSON值为null? 例如: [{用户名:空},{用户名:空}]

第一种情况再presents的unexisting用户名并命名为空的第二个用户。但是,如果你尝试检索他们两个值导致在字符串中的空

 的JSONObject JSON =新的JSONObject({\你好\:空});
json.put(再见,JSONObject.NULL);
Log.e(日志,json.toString());
Log.e(日志,你好=+ json.getString(你好)+为空?
                +(json.getString(你好)== NULL));
Log.e(日志,再见=+ json.getString(再见)+为空?
                +(json.getString(再见)== NULL));
 

日志输出

  {你好:空,再见:空}
你好= null是空?假
再见= null是空?假
 

解决方案

试着用 json.isNull(字段名)

参考:<一href="http://developer.android.com/reference/org/json/JSONObject.html#isNull%28java.lang.String%29">http://developer.android.com/reference/org/json/JSONObject.html#isNull%28java.lang.String%29

How can I detect when a json value is null? for example: [{"username":null},{"username":"null"}]

The first case represents an unexisting username and the second a user named "null". But if you try to retrieve them both values result in the string "null"

JSONObject json = new JSONObject("{\"hello\":null}");
json.put("bye", JSONObject.NULL);
Log.e("LOG", json.toString());
Log.e("LOG", "hello="+json.getString("hello") + " is null? "
                + (json.getString("hello") == null));
Log.e("LOG", "bye="+json.getString("bye") + " is null? "
                + (json.getString("bye") == null));

The log output is

{"hello":"null","bye":null}
hello=null is null? false
bye=null is null? false

解决方案

Try with json.isNull( "field-name" ).

Reference: http://developer.android.com/reference/org/json/JSONObject.html#isNull%28java.lang.String%29

这篇关于Android的JSON和空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆