
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
May 29th, 2025
Choosing the right programming language can make all the difference — whether you’re building cutting-edge AI, launching a startup, or simply looking to future-proof your skills. The year 2025 has […]
Read more
May 6th, 2025
Congratulations on embarking on this incredible journey of pregnancy! As your body changes and your little one grows, you might find yourself seeking reliable information and tools to guide you. […]
Read more
May 1st, 2025
The mobile app industry is changing quickly, and Singapore is leading the way in innovation across Southeast Asia. With 2025 on the horizon, businesses must keep up with the latest […]
Read more