So, I get some JSON values from the server but I don't know if there will be a particular field or not.
因此,我从服务器获得一些JSON值,但我不知道是否会有一个特定字段。
So like:
就像:
{ "regatta_name":"ProbaRegatta",
"country":"Congo",
"status":"invited"
}
And sometimes, there will be an extra field like:
有时,会有一个额外的领域,比如:
{ "regatta_name":"ProbaRegatta",
"country":"Congo",
"status":"invited",
"club":"somevalue"
}
I would like to check if the field named "club" exists so that at parsing I won't get org.json.JSONException: No value for club
我想检查名为“club”的字段是否存在,以便在解析时不会获得org.json。JSONException:俱乐部没有价值。
8 个解决方案
#1
184
JSONObject class has a method named "has":
JSONObject类有一个名为“has”的方法:
http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
http://developer.android.com/reference/org/json/JSONObject.html(以)
Returns true if this object has a mapping for name. The mapping may be NULL.
如果该对象具有名称映射,则返回true。映射可能为空。
#2
108
You can check this way where 'HAS' - Returns true if this object has a mapping for name. The mapping may be NULL.
您可以这样检查'HAS' -如果这个对象有名称的映射返回true。映射可能为空。
if (json.has("status")) {
String status = json.getString("status"));
}
if (json.has("club")) {
String club = json.getString("club"));
}
You can also check using 'isNull' - Returns true if this object has no mapping for name or if it has a mapping whose value is NULL.
您还可以使用'isNull'检查—如果这个对象没有名称映射,或者它有一个值为NULL的映射,则返回true。
if (!json.isNull("club"))
String club = json.getString("club"));
#3
10
you could JSONObject#has
, providing the key
as input and check if the method returns true
or false
. You could also
您可以将JSONObject#作为输入提供键,并检查方法是否返回true或false。你也可以
use optString
instead of getString
:
使用optString代替getString:
Returns the value mapped by name if it exists, coercing it if necessary. Returns the empty string if no such mapping exists
返回按名称映射的值(如果存在),必要时强制它。如果不存在这种映射,则返回空字符串
#4
8
You can use has
您可以使用了
public boolean has(String key)
Determine if the JSONObject contains a specific key.
确定JSONObject是否包含特定的键。
Example
例子
JSONObject JsonObj = new JSONObject(Your_API_STRING); //JSONObject is an unordered collection of name/value pairs
if (JsonObj.has("address")) {
//Checking address Key Present or not
String get_address = JsonObj .getString("address"); // Present Key
}
else {
//Do Your Staff
}
#5
6
just before read key check it like before read
在阅读前,像阅读前一样检查它
JSONObject json_obj=new JSONObject(yourjsonstr);
if(json_obj.isNull("club"))
{
//it's contain value to be read operation
}
else
{
//it's not contain key club or isnull so do this operation here
}
isNull function definition
isNull函数定义
Returns true if this object has no mapping for name or
if it has a mapping whose value is NULL.
official documentation below link for isNull
function
isNull函数的链接下面的官方文档
http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String)
http://developer.android.com/reference/org/json/JSONObject.html isNull(以)
#6
2
Try this
试试这个
if(!jsonObj.isNull("club")){
jsonObj.getString("club");
}
#7
2
A better way, instead of using a conditional like:
更好的方法,而不是使用条件类:
if (json.has("club")) {
String club = json.getString("club"));
}
is to simply use the existing method optString(), like this:
是简单地使用现有的方法optString(),如下所示:
String club = json.optString("club);
the optString("key") method will return an empty String if the key does not exist and won't, therefore, throw you an exception.
optString(“key”)方法将返回一个空字符串,如果该键不存在,因此不会抛出异常。
#8
0
I used hasOwnProperty('club')
var myobj = { "regatta_name":"ProbaRegatta",
"country":"Congo",
"status":"invited"
};
if ( myobj.hasOwnProperty("club"))
// do something with club (will be false with above data)
var data = myobj.club;
if ( myobj.hasOwnProperty("status"))
// do something with the status field. (will be true with above ..)
var data = myobj.status;
works in all current browsers.
适用于所有当前浏览器。
#1
184
JSONObject class has a method named "has":
JSONObject类有一个名为“has”的方法:
http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
http://developer.android.com/reference/org/json/JSONObject.html(以)
Returns true if this object has a mapping for name. The mapping may be NULL.
如果该对象具有名称映射,则返回true。映射可能为空。
#2
108
You can check this way where 'HAS' - Returns true if this object has a mapping for name. The mapping may be NULL.
您可以这样检查'HAS' -如果这个对象有名称的映射返回true。映射可能为空。
if (json.has("status")) {
String status = json.getString("status"));
}
if (json.has("club")) {
String club = json.getString("club"));
}
You can also check using 'isNull' - Returns true if this object has no mapping for name or if it has a mapping whose value is NULL.
您还可以使用'isNull'检查—如果这个对象没有名称映射,或者它有一个值为NULL的映射,则返回true。
if (!json.isNull("club"))
String club = json.getString("club"));
#3
10
you could JSONObject#has
, providing the key
as input and check if the method returns true
or false
. You could also
您可以将JSONObject#作为输入提供键,并检查方法是否返回true或false。你也可以
use optString
instead of getString
:
使用optString代替getString:
Returns the value mapped by name if it exists, coercing it if necessary. Returns the empty string if no such mapping exists
返回按名称映射的值(如果存在),必要时强制它。如果不存在这种映射,则返回空字符串
#4
8
You can use has
您可以使用了
public boolean has(String key)
Determine if the JSONObject contains a specific key.
确定JSONObject是否包含特定的键。
Example
例子
JSONObject JsonObj = new JSONObject(Your_API_STRING); //JSONObject is an unordered collection of name/value pairs
if (JsonObj.has("address")) {
//Checking address Key Present or not
String get_address = JsonObj .getString("address"); // Present Key
}
else {
//Do Your Staff
}
#5
6
just before read key check it like before read
在阅读前,像阅读前一样检查它
JSONObject json_obj=new JSONObject(yourjsonstr);
if(json_obj.isNull("club"))
{
//it's contain value to be read operation
}
else
{
//it's not contain key club or isnull so do this operation here
}
isNull function definition
isNull函数定义
Returns true if this object has no mapping for name or
if it has a mapping whose value is NULL.
official documentation below link for isNull
function
isNull函数的链接下面的官方文档
http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String)
http://developer.android.com/reference/org/json/JSONObject.html isNull(以)
#6
2
Try this
试试这个
if(!jsonObj.isNull("club")){
jsonObj.getString("club");
}
#7
2
A better way, instead of using a conditional like:
更好的方法,而不是使用条件类:
if (json.has("club")) {
String club = json.getString("club"));
}
is to simply use the existing method optString(), like this:
是简单地使用现有的方法optString(),如下所示:
String club = json.optString("club);
the optString("key") method will return an empty String if the key does not exist and won't, therefore, throw you an exception.
optString(“key”)方法将返回一个空字符串,如果该键不存在,因此不会抛出异常。
#8
0
I used hasOwnProperty('club')
var myobj = { "regatta_name":"ProbaRegatta",
"country":"Congo",
"status":"invited"
};
if ( myobj.hasOwnProperty("club"))
// do something with club (will be false with above data)
var data = myobj.club;
if ( myobj.hasOwnProperty("status"))
// do something with the status field. (will be true with above ..)
var data = myobj.status;
works in all current browsers.
适用于所有当前浏览器。