JSON 对象中的值未被访问
Value in JSON Object not being accessed
我有 JSON 对象 contacts
看起来像这样 {"email":"Mark@HookahStationBCS.com","phone":"+19796918899"}
在我的代码中,我获取了 phone
键并能够获取它的值,但是当我打印出 email
值,它说它是空的,在我下面的代码中它实际上并没有通过终止案例。有谁知道为什么?
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);
}
}
你有一个 elseif 条件。你必须有 2 个 if 条件。我几乎是在说之前发帖者所说的,只是措辞不同。
我认为你的代码应该是这样的:
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);
}
}
我有 JSON 对象 contacts
看起来像这样 {"email":"Mark@HookahStationBCS.com","phone":"+19796918899"}
在我的代码中,我获取了 phone
键并能够获取它的值,但是当我打印出 email
值,它说它是空的,在我下面的代码中它实际上并没有通过终止案例。有谁知道为什么?
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);
}
}
你有一个 elseif 条件。你必须有 2 个 if 条件。我几乎是在说之前发帖者所说的,只是措辞不同。
我认为你的代码应该是这样的:
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);
}
}