i'm trying to get Double value from json, i want to ask how to check double value is empty or not?
我想从json获得Double值,我想问一下如何检查double值是否为空?
this is how i initial it private static
这是我初始化私有静态的方式
final String kartu_xcord = "xcord";
private static final String kartu_ycord = "ycord";
ouble xcord,ycord;
this is how i parse my json:
这是我解析我的json的方式:
xcord = c.getDouble(kartu_xcord);
ycord = c.getDouble(kartu_ycord);
this is how i try to check xcord is null or empty :
这是我试图检查xcord是null还是空:
if (xcord.isNaN()) {
LinlayNoMap.setVisibility(View.VISIBLE);
} else {
linlaymap.setVisibility(View.VISIBLE);
}
i hope someone can help me to solve my proble thank you
我希望有人能帮我解决问题谢谢你
7 个解决方案
#1
11
Make it to Double
not double
and check if Double
value is null, in java double
value can not be null.
使它成为Double而不是double并检查Double值是否为null,在java double值中不能为null。
#2
3
In Java double
cannot be null
.
在Java中,double不能为null。
#3
1
It can't be null or empty. It's a double value. Maybe try the Double wrapper?
它不能为null或为空。这是双重价值。也许试试Double包装?
#4
1
You could check if your json has the key you area looking for:
您可以检查您的json是否具有您所在区域的关键字:
if (c.hasDouble("my_coord")) {
y_coord = c.getDouble("my_coord");
}
if y_coord is a Double
(with capital D), you can set it to null.
如果y_coord是Double(使用大写D),则可以将其设置为null。
if (c.hasDouble("my_coord")) {
y_coord = c.getDouble("my_coord");
} else {
y_coord = null;
}
This depends of course on the API you are using to read your JSON
, the hasDouble
method could be called something else. I recommend you give a try to GSON
, very simple and will handle most of the parsing work for you.
这当然取决于您用来读取JSON的API,hasDouble方法可以被称为其他方法。我建议你试试GSON,非常简单,并将为你处理大部分的解析工作。
#5
0
doubles can never be null in Java. You can instead use the hasKey
method of Json to check if the item you're parsing contains the value.
Java中的双精度数永远不会为空。您可以改为使用Json的hasKey方法来检查您正在解析的项是否包含该值。
c.hasKey("xcord")
#6
0
assume you are using JSONObject, you can do this
假设您正在使用JSONObject,您可以这样做
xcord = c.optDouble(kartu_xcord, Double.MIN_VALUE);
if (xcord == Double.MIN_VALUE){
//xcord is empty
}
#7
-1
Use Double, not double.
使用Double,而不是double。
Double dValue= someVariable;
if(!dValue.isNaN())
{
// you code here
}
#1
11
Make it to Double
not double
and check if Double
value is null, in java double
value can not be null.
使它成为Double而不是double并检查Double值是否为null,在java double值中不能为null。
#2
3
In Java double
cannot be null
.
在Java中,double不能为null。
#3
1
It can't be null or empty. It's a double value. Maybe try the Double wrapper?
它不能为null或为空。这是双重价值。也许试试Double包装?
#4
1
You could check if your json has the key you area looking for:
您可以检查您的json是否具有您所在区域的关键字:
if (c.hasDouble("my_coord")) {
y_coord = c.getDouble("my_coord");
}
if y_coord is a Double
(with capital D), you can set it to null.
如果y_coord是Double(使用大写D),则可以将其设置为null。
if (c.hasDouble("my_coord")) {
y_coord = c.getDouble("my_coord");
} else {
y_coord = null;
}
This depends of course on the API you are using to read your JSON
, the hasDouble
method could be called something else. I recommend you give a try to GSON
, very simple and will handle most of the parsing work for you.
这当然取决于您用来读取JSON的API,hasDouble方法可以被称为其他方法。我建议你试试GSON,非常简单,并将为你处理大部分的解析工作。
#5
0
doubles can never be null in Java. You can instead use the hasKey
method of Json to check if the item you're parsing contains the value.
Java中的双精度数永远不会为空。您可以改为使用Json的hasKey方法来检查您正在解析的项是否包含该值。
c.hasKey("xcord")
#6
0
assume you are using JSONObject, you can do this
假设您正在使用JSONObject,您可以这样做
xcord = c.optDouble(kartu_xcord, Double.MIN_VALUE);
if (xcord == Double.MIN_VALUE){
//xcord is empty
}
#7
-1
Use Double, not double.
使用Double,而不是double。
Double dValue= someVariable;
if(!dValue.isNaN())
{
// you code here
}