我正在开发一个应用程序,我在其中调用一个服务,其中数据以数组格式出现。 如下图所示:
[
{
"Image":null,
"FirstName":null,
"Active":false,
"HallTicketNumber":0,
"ReportingTime":null,
"EventDetails":null,
"CompanyName":null,
"Designation_Role":null,
"EventDate":null,
"StateName":null,
"CityName":null,
"FullAddress":null,
"HallTicket_Status":"HETSTOP"
}
]
因为我需要检查 HET_STATUS 的条件,如果该状态 HET STOP 那么它应该打印出het stop。但当我试图检查条件时它没有显示任何内容:
以下是我的代码:
String StringData = "" + data;
try {
JSONArray rootArray = new JSONArray(StringData);
int len = rootArray.length();
for (int i = 0; i < len; ++i) {
JSONObject json = rootArray.optJSONObject(i);
String CompanyName = json.optString("CompanyName ");
String Position = json.optString("Designation_Role");
String EventDate = json.optString("EventDate");
String StateName = json.optString("StateName ");
String CityName = json.optString("CityName");
String Address = json.getString("FullAddress");
String ReportingTime = json.getString("ReportingTime");
String HallTicket_Status = json.getString("HallTicket_Status");
if (HallTicket_Status == "HETSTOP") {
Toast toast = Toast.makeText(UpcomingEventsDetails.this, "HET STOP", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
答案 0 :(得分:1)
试试这种方式,
if(HallTicket_Status.equalsIgnoreCase("HETSTOP")){
Toast toast = Toast.makeText(UpcomingEventsDetails.this, "HET STOP", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}else{
//do something
}
boolean equals(String str):区分大小写的比较
boolean equalsIgnoreCase(String str):区分大小写比较
==
仍将测试对象平等。然而,很容易被愚弄:
Integer a = 10;
Integer b = 10;
System.out.println(a == b); //prints true
Integer c = new Integer(10);
Integer d = new Integer(10);
System.out.println(c == d); //prints false. however:
Integer a = 10;
Integer b = 10;
System.out.println(a == b); //prints true
Integer c = new Integer(10);
Integer d = new Integer(10);
System.out.println(c == d); //prints false
答案 1 :(得分:0)
不要将==
条件用于字符串。而不是使用str.equals("HEYSTOP")
。这将返回布尔值。如果您检查字符串是否为空,那么您应该使用==
或!=
条件。