
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
Nov 12th, 2026
Singapore’s real estate market is becoming increasingly technology-driven. From residential condominiums and commercial buildings to serviced apartments and rental properties, managing real estate involves coordinating multiple activities, including tenant communication, […]
Read more
Oct 29th, 2026
Building a mobile app with advanced features may attract users initially, but getting them to return regularly is a different challenge. Many businesses invest in sophisticated functionality, attractive interfaces, and […]
Read more
Oct 13th, 2026
Singapore’s tropical climate can change quickly, with bright sunshine giving way to sudden showers, thunderstorms, or heavy rainfall. For residents, commuters, tourists, and businesses, reliable weather information can make everyday […]
Read more