我有contacts
一个看起来像这样的JSON对象,{"email":"[email protected]","phone":"+19796918899"}
在我的代码中,我抓住了phone
键并能够获取它的值,但是当我打印出该email
值时,它说它为null,而在下面的代码中,它实际上没有通过终止条件。有人知道为什么吗?
if(nextActivityObject.getJSONObject("contacts") != null){
Log.d("NEXT ACTIVITY OBJECT NOT EMPTY", "NOT EMPTY");
contacts = nextActivityObject.getJSONObject("contacts");
if(contacts.getString("phone") != null){
phone = contacts.getString("phone");
Log.d("PHONE PASSED", phone.toString());
intent.putExtra("phone",phone);
} else if(contacts.getString("email") != null){
email = contacts.getString("email");
Log.d("THIS IS THE EMAIL PASS", email.toString());
intent.putExtra("email", email);
}
}
我认为您的代码应该是这样的:
if (nextActivityObject.getJSONObject("contacts") != null){
Log.d("NEXT ACTIVITY OBJECT NOT EMPTY", "NOT EMPTY");
contacts = nextActivityObject.getJSONObject("contacts");
if(contacts.getString("phone") != null && contacts.getString("email") != null){
phone = contacts.getString("phone");
email = contacts.getString("email");
intent.putExtra("phone",phone);
intent.putExtra("email",email);
}
else if (contacts.getString("phone") != null){
phone= contacts.getString("phone");
intent.putExtra("phone", phone);
}
else if (contacts.getString("email") != null){
email = contacts.getString("email");
intent.putExtra("email", email);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句