Efficient way of parsing and validating JSON

3 minutes read

JSON

JSON is very familiar for programmers. Basically it stands for JavaScript Object Notation. It is light weighted, well structured. It is also easy to parse and readable. It is a good alternative to XML when we required to get data from server. In this tutorial we will use an efficient way to parse JSON and validate it.

 

//Here first we will create a class named JsonParser

 

public class JsonParser {

                ……………………..

                ……………………..

}

 

/*            Here first we will validate JSON object.

                If it is valid our method will return true other wise false.

*/

public static boolean isJsonObject(String data) {

                                try {

                                                new JSONObject(data); //create a new object.

                                                return true;

                                } catch (Exception e) {

                                                e.printStackTrace();

                                                return false;

                                }

                }

 

 

/*            Here first we will validate JSON Array.

                If it is valid our method will return true other wise false.

*/

public static boolean isJsonArray(String data) {

                                try {

                                                new JSONArray(data); //create a new JSON array object.

                                                return true;

                                } catch (Exception e) {

                                                e.printStackTrace();

                                                return false;

                                }

                }

 

/*            Here first we will retrieve String value from JSON object with given key.

                This method will check existence of key in given json object, if it exists with some value, value will be returned       other wise blank value will be returned.

*/

public static String getString(JSONObject object, String key) {

                                try {

                                                if (object.has(key)) {

                                                                String value = object.getString(key);

                                                                if (TextUtils.isEmpty(value)) {

                                                                                return “”;

                                                                } else {

                                                                                if(value.equalsIgnoreCase(“null”))

                                                                                                return “”;

                                                                                else

                                                                                   return value;

                                                                }

                                                } else {

                                                                return “”;

                                                }

                                } catch (Exception e) {

                                                e.printStackTrace();

                                                return “”;

                                }

                }

 

 

/*            Here first we will retrieve integer value from JSON object with given key.

                This method will check existence of key in given json object, if it exists with some value, value will be returned       other wise 0 will be returned.

*/

public static int getInt(JSONObject object, String key) {

                                try {

                                                if (object.has(key)) {

                                                                int value = object.getInt(key);

                                                                                return value;

                                                } else {

                                                                return 0;

                                                }

                                } catch (Exception e) {

                                                e.printStackTrace();

                                                return 0;

                                }

                }

  

Now we will use these methods in our program.

 

// suppose we have following JSON data.

 

{

“location”:

   [

                {

            “city”:”New Delhi”,

                “latitude”:”28.6357600“,

                “longitude”:”77.2244500“

                },

                {

                “city”:”Lucknow”,

                “latitude”:”26.8500000 “,

                “longitude”:”80.9166700 “

                },

                {

                “city”:”Mumbai”,

                “latitude”:”19.0144100 “,

                “longitude”:”72.8479400 “

                },

                {

                “city”:”Bangalore”,

                “latitude”:”12.9762300“,

                “longitude”:”77.6032900 “

                },

 

   ]

}

 

First of all we will check validity of this json string.

Suppose we have this json string in a variable name “jasondata”

 

String jsondata=”…………”;

 

if(JsonParser.isJsonObject(jsondata))

{

JSONObject jObject = new JSONObject(jsondata);

                if(JsonParser.isJsonArray(jObject,”location”))

                {

                                JSONArray jArray = jObject.getJSONArray(“location”);

                                for (int i = 0; i < jArray.length(); i++) {

                                                JSONObject arrayElement= jArray.getJSONObject(i);

                                                String city=JsonParser.getString (arrayElement,”city”);

                                                String latitude=JsonParser.getString (arrayElement,”latitude”);

                                                String longitude=JsonParser.getString (arrayElement,”longitude”);

                                }

                }

}

else

Toast.makeText(getApplicationContext(),”Invalid response from server”,Toast.LENGTH_SHORT).show();

 

Related Posts...

GeneralMobile AppsTechnologies