7 Best Astrology Apps for Accurate Horoscopes
Millions of people across the globe turn to astrology for guidance in love, career, health, and personal growth. A survey by Pew Research Center found that 29% of Americans believe […]
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();
GeneralMobile AppsTechnologies
Feb 20th, 2025
Millions of people across the globe turn to astrology for guidance in love, career, health, and personal growth. A survey by Pew Research Center found that 29% of Americans believe […]
Feb 11th, 2025
Remember when your parents warned you never to get into a car with a stranger? Fast forward to today, and getting into a stranger’s car has become a global business […]
Feb 6th, 2025
In today’s digital age, convenience is king, and online gift delivery services are thriving. With the global online gift market expected to grow significantly, now is the perfect time to […]