【问题标题】:Check if condition from json检查来自json的条件
【发布时间】:2020-08-08 09:39:09
【问题描述】:

我有一个 int 值,比如说 10,现在我想检查 10 是否小于 50,所以我将 if 条件写为: 如果(10

【问题讨论】:

  • 试试,int number = jsonObject.getInt("number"); if(10
  • 或者您正在寻找这个? int something = Integer.parseInt(number);然后在 if 语句中使用一些东西
  • 检查 String.charAt(position) 是否有帮助?

标签: android json string if-statement


【解决方案1】:

如果您确定 JSON 响应将采用以下格式 -

*characterNUMBER*

那么就可以得到int的值如下-

    String test = "<50";
    int value1 = Integer.parseInt(test.replace("<", ""));

    /*
    * Using substring method
    * String.subString(beginIndex, endIndex);
    * Note: beginIndex is inclusive but endIndex is the exclusive
    * */
    int value2 = Integer.parseInt(test.substring(1,test.length()));

value1value2 都将从该字符串中提取 int。

为了便于使用,请在您的 Json POJO 中创建一个方法来返回整数值-

public class JSONResponce{
    
    .....

    @SerializedName("some_string_value")
    private String stringValue;

    public int getIntValue(){
        //do conversion of String to actual int here
        return Integer.parseInt(stringValue.substring(1,stringValue.length()));
    }
    ....
}

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 2018-02-08
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多