-2

I have a json which has a tag "msg" . Now if search results are shown in json then this tag is not there and when search results are 0 then this tag comes with value not found.

Now i want to keep a check whether this tag is present or not. So how to do so

Here is the sample of json when no records found :

{"product":[{"msg":"Not Found"}]}
nanoweb
  • 125
  • 1
  • 2
  • 12

5 Answers5

1

first check "msg" tag is persent or not then you have to check string

try {

     if(yourjsonObject.has("msg")){ 
    {
                  if (json.getString("msg").equals("Not Found")) {  
                  // do some thing
                  }
                  else{
                  // do some thing else  
                  }
    }
   } catch (JSONException e) {
         e.printStackTrace(); 
 }
Jignesh Jain
  • 1,518
  • 12
  • 28
  • mapping is marking a value to any tag right? but if the tag itself is not present then how can we check for mapping?.. correct me if i am wrong – nanoweb May 21 '15 at 05:15
  • please check my answer first i have check tag itself present or not then we check for message. – Jignesh Jain May 21 '15 at 05:17
1

Please Check the below solution

try 
{ 
JSONObject obj=new JSONObject(respons);//respons is the response of the yours web sevice
JSONArray inner_json_array = obj.getJSONArray("product");
for(int j=0;j<inner_json_array.length();j++) {
   if(inner_json_array.getJSONObject(j).has("msg"))
    {
     String msg= inner_json_array.getJSONObject(j).getString("msg");
      if (msg.equals("Not Found")) {
      // do some thing
        }
      else{
      // do some thing else  
      }
    }
  }     
 } catch (JSONException e) {
        e.printStackTrace(); 
}   
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • 1
    i guess this will throw an error cz the tag itself is not present . i.e msg and you are trying to fetch its value – nanoweb May 21 '15 at 04:57
0

You can check if there is value mapped to key using optString(). It returns an empty string if there is no such key. If the value is not a string and is not null, then it is converted to a string. Using getString() will give a JSONException if there is no corresponding key.

jsonObject.optString("msg")

http://developer.android.com/reference/org/json/JSONObject.html#optString(java.lang.String)

dishooom
  • 498
  • 4
  • 9
  • Have you tried used optString() wherever getString() was used.? Can you post the part of JSON where you are using 'category' ? – dishooom May 21 '15 at 05:09
  • ok but mapping is marking a value to any tag right? but if the tag itself is not present then how can we check for mapping?.. correct me if i am wrong – nanoweb May 21 '15 at 05:14
  • public java.lang.String optString(java.lang.String key, java.lang.String defaultValue) Get an optional string associated with a key. It returns the defaultValue if there is no such key. Parameters: key - A key string. defaultValue - The default. Returns: A string which is the value. If there is no key, it will not throw an exception, it will just supply default value – dishooom May 21 '15 at 05:16
0

Try this way :

try {
          if (json.getString("msg").compareTo("Not Found") == 0) {
          // do some thing
          }
          else{
          // do some thing else  
          }

      } catch (JSONException e) {
            e.printStackTrace(); 
      }
YFeizi
  • 1,498
  • 3
  • 28
  • 50
0

Your complete solution will be like this

JSONObject obj=new JSONObject(response);//This is response from webservice
JSONArray json_array = obj.getJSONArray("product");
for(int j=0;j<json_array.length();j++) {
    if(json_array.getJsonObject(j).has("msg")) {
     // if found
    } else {
     // if not found
    }
}

This surely will help.

activesince93
  • 1,716
  • 17
  • 37