Reputation: 183
I am using the condition -
if (!obj2.getString("patronCheckoutInfo").equals("null")) {
But it does not work for the case when array got the value.
Eg: Case1- when json got value for "patronCheckoutInfo":
{
"ErrorMessage": ""
"Message": "Operation completed successfully"
"Status": "OK"
"Results": {
"LookupPatronInfoResponse": {
"patronAddressInfo": null
"patronCheckoutHistoryInfo": null
"patronCheckoutInfo": [6]
0: {
"author": "Cobb, Kevin."
Case 2 - when no value:
{
"ErrorMessage": ""
"Message": "Operation completed successfully"
"Status": "OK"
"Results": {
"LookupPatronInfoResponse": {
"patronAddressInfo": null
"patronCheckoutHistoryInfo": null
"patronCheckoutInfo": null
"patronCirculationInfo": null
Tried few tutorials but unable to figure out. Any suggestion is welcome. Thanks in advance.
Upvotes: 3
Views: 10187
Reputation: 793
You can try like this:
JSONArray patronCheckoutInfo= obj2.optJSONArray("patronCheckoutInfo");
if(patronCheckoutInfo){
for (int i = 0; i < invitations.length(); i++) {
JSONObject patronCheckoutInfoObject= patronCheckoutInfo.getJSONObject(i);
}
}
Upvotes: 0
Reputation: 183
Thanks all for the suggestion. Below worked for me:
if (obj2.getString("patronCheckoutInfo") != null)
Upvotes: 0
Reputation: 4907
Try with
if (!obj2.getString("patronCheckoutInfo").equals("null"))
to
String patronCheckoutInfo = obj2.getString("patronCheckoutInfo");
if(!TextUtils.isEmpty(patronCheckoutInfo)){
// string has value
}else{
// string is empty
}
Upvotes: 0
Reputation: 4781
Yes, you can check if the particular key present in the Json.
Its pretty simple..!! Try this..
boolean response = jsonobject.has("patronCheckoutInfo")
Hope this will help you, Happy coding
Upvotes: 2
Reputation: 1819
You can use support function isNull
(http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String))
So, re-write your code in this case:
if (!obj2.isNull("patronCheckoutInfo")) {
// do something
}
Upvotes: 3