2017-02-22 40 views
-2

我有一个问题我想比较我的JSON,但不是工作我不明白为什么。 我尝试使用函数compareTo(),但不工作。我想这我怎么能比较一个JSON < or >

如果(Myjson < 5或Myjson> 5)

{(做某事)}

我告诉我的代码

Acceuil.java

public class Accueil extends AppCompatActivity { 
String json_string; 
JSONObject jObj = null; 
private TextView Mpx,Al,Ar,Rds,Pilots,Frequence,Rf; 
private String value= String.valueOf(5); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_accueil); 
    json_string = getIntent().getExtras().getString("json_data"); 

    Mpx = (TextView) findViewById(R.id.mpx); 
    Ar=(TextView)findViewById(R.id.ar); 
    Al=(TextView)findViewById(R.id.al); 
    Rds=(TextView)findViewById(R.id.rds); 
    Pilots=(TextView)findViewById(R.id.pilots); 
    Frequence=(TextView)findViewById(R.id.fréquence); 
    Rf=(TextView)findViewById(R.id.rf); 

      try { 
       // Json Object {} 
       /******************************************************************************/ 

       String mpx, rds, al, ar, frequence, pilots, id, id_SIGFOX, timestamps, rf; 
       jObj = new JSONObject(json_string); 
       mpx = jObj.getString("MPX"); 
       rds = jObj.getString("RDS"); 
       rf = jObj.getString("RF"); 
       frequence = jObj.getString("Frequence"); 
       timestamps = jObj.getString("timestamp"); 
       id = jObj.getString("id"); 
       id_SIGFOX = jObj.getString("id_SIGFOX"); 
       pilots = jObj.getString("PILOT"); 
       al = jObj.getString("a_l"); 
       ar = jObj.getString("a_r"); 
       Valeur valeurs = new Valeur(mpx, rds, al, ar, frequence, pilots, id, timestamps, id_SIGFOX, rf); 
       /******************************************************************************/ 
       if(mpx.compareTo(String.valueOf(5))) { 
       Mpx.setText(valeurs.getMpx()) 
       Mpx.setText(valeurs.getMpx()); 
       Mpx.setTextColor(Color.parseColor("#00000")) 

       }else{ 
       Mpx.setText(valeurs.getMpx()) 
       Mpx.setTextColor(Color.parseColor("#DF0101"))};//red 

      } catch (JSONException e) { 

       e.printStackTrace(); 
      } 

     } 

public void popUp(View view){ 
    Intent intent=new Intent(this,popUp.class); 
    startActivity(intent); 

} 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.warning: 
      Intent intent = new Intent(this, popUp.class); 
      startActivity(intent); 
      return true; 
     case R.id.localiser: 
      return true; 
     case R.id.station: 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    //ajoute les entrées de menu_test à l'ActionBar 
    getMenuInflater().inflate(R.menu.menu, menu); 
    return true; 
} 
} 

Valeur .java

public class Valeur { 
    private String mpx,rds,al,ar,pilots,frequence,id,timestamps,id_SIGFOX,rf; 
    public Valeur(String mpx, String rds, String al, String ar, String pilots, String frequence, String id, String timestamps, String id_SIGFOX, String rf) 

    { 
     this.setMpx(mpx); 
     this.setRds(rds); 
     this.setAl(al); 
     this.setAr(ar); 
     this.setPilots(pilots); 
     this.setFrequence(frequence); 
     this.setId(id); 
     this.setTimestamps(timestamps); 
     this.setId_SIGFOX(id_SIGFOX); 
     this.setRf(rf); 

    } 




    public String getMpx() { 
    return mpx; 
} 

    public void setMpx(String mpx) { 
    this.mpx = mpx; 
} 

    public String getRds() { 
    return rds; 
} 

    public void setRds(String rds) { 
    this.rds = rds; 
} 

    public String getAl() { 
    return al; 
} 

    public void setAl(String al) { 
    this.al = al; 
} 

    public String getAr() { 
    return ar; 
} 

    public void setAr(String ar) { 
    this.ar = ar; 
} 

    public String getPilots() { 
    return pilots; 
} 

    public void setPilots(String pilots) { 
    this.pilots = pilots; 
} 

    public String getFrequence() { 
    return frequence; 
} 

    public void setFrequence(String frequence) { 
    this.frequence = frequence; 
} 

    public String getTimestamps() { 
    return timestamps; 
} 

    public void setTimestamps(String timestamps) { 
    this.timestamps = timestamps; 
} 

    public String getId() { 
    return id; 
} 

    public void setId(String id) { 
    this.id = id; 
} 

    public String getId_SIGFOX() { 
    return id_SIGFOX; 
} 

    public void setId_SIGFOX(String id_SIGFOX) { 
    this.id_SIGFOX = id_SIGFOX; 
} 

    public String getRf() { 
    return rf; 
} 

    public void setRf(String rf) { 
    this.rf = rf; 
} 
} 

我通量JSON

{"id":"1","timestamp":"2017-01-31 10:59:11","id_SIGFOX":"ABER","MPX":"1","RDS":"2","RF":"79","PILOT":"8","a_l":"-5","a_r":"-39","Frequence":"1034"} 
+0

所以,你试图比较''使用''>''和''<''String''值?那么''“Hello”<“World”''的结果是什么?那是“真”还是“假”? – f1sh

+0

HI?我想比较我的价值mpx(谁是1)在5所以我想,如果(mpx(1)<5)我将编辑显示我的流量json –

+0

您必须将json的字符串值转换为整数比较 – Blip

回答

0

做如下:

int mpxInt = Integer.parseInt(mpx); 
if(mpxInt > 5) { 
    // mpx is > 5 
} else { 
    // mpx is < 5 
} 

这应该工作正常

+0

thx现在正在工作 –

0

我认为这个问题是您尝试将字符串与整数5

mpx = jObj.getString("MPX"); 
... 
if(mpx.compareTo(String.valueOf(5))) { 

比较mpx为了正确地做到这一点,你需要转换mpx为int。或者使用别的而不是jObj.getString,或者使用Integer.parseInt将字符串转换为int。

+0

thx现在正在工作 –