
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
Jul 23rd, 2026
Moving to Singapore is an exciting opportunity, whether you’re relocating for work, education, or business. Known for its modern infrastructure, efficient public services, and multicultural environment, Singapore offers an excellent […]
Read more
Jul 21st, 2026
Launching an app is exciting, but getting the dreaded “Your app has been rejected” email from Apple can be frustrating. In 2026, the App Store review process has become stricter […]
Read more
Jul 7th, 2026
The rapid growth of digital payments has transformed how businesses and consumers manage financial transactions. Whether you’re ordering food, shopping online, paying utility bills, or transferring money internationally, secure digital […]
Read more