Parag Agrawal Parag Agrawal - 1 year ago 82
Android Question

Android Volley JSON parsing issue Null checking

I have an issue where I am parsing JSON object and I have 22 elements in each object and I want to null check all of them. I tried to search for a solution but everywhere its just the solution to check each of them one by one. Is there any way to check if any of the elements in my JSON objects is Null as is a very lengthy procedure to do that individually to each one of the 22 elements. Below is the code for my JSON response.

public void onResponse(JSONArray response) {
Log.i("response data", response.toString());
final ArrayList<DataPOJO> list = new ArrayList<>();
try {
JSONArray jArray = response;
Log.i("jArray data", jArray.toString());
for (int i = 0; i < jArray.length(); i++){
JSONObject singleItem = jArray.getJSONObject(i);
DataPOJO data = new DataPOJO();
data.zipcode = singleItem.getInt("zipcode");
data.latitude = singleItem.getDouble("latitude");
data.longitude = singleItem.getDouble("longitude");
data.year = singleItem.getInt("year");
data.make = singleItem.getString("make");
data.model = singleItem.getString("model");
data.transmission = singleItem.getString("transmission");
data.odometer = singleItem.getDouble("odometer");
data.style = singleItem.getString("style");
data.carDescription = singleItem.getString("carDes");
data.advanceNotice = singleItem.getString("advNotice");
data.trim = singleItem.getString("trim");
//data.image = singleItem.getString("carPic");
//data.licenseNumber = singleItem.getString("licenseNo");
//data.issuingCountry = singleItem.getString("issuingCountry");
//data.issuingState = singleItem.getString("issuingState");
//data.licensePlateNumber = singleItem.getString("licensePlateNum");
//data.licenseState = singleItem.getString("licenseState");
//data.lName = singleItem.getString("lNameOnLic");
//data.fName = singleItem.getString("fNameOnLic");
data.minimumDuration = singleItem.getString("shortPT");
data.longestDistance = singleItem.getString("longPT");
list.add(data);
}
} catch (JSONException e) {
Log.e("JSONException: ", e.getMessage());
e.printStackTrace();
}
Log.i("Array list data:",list.toString());
ListView lst = (ListView) findViewById(R.id.car_list);
ListViewAdapter adapter = new ListViewAdapter(list,getApplicationContext());
lst.setAdapter(adapter);

lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent carChoiceIntent = new Intent(getApplicationContext(), CarChoice.class);
carChoiceIntent.putExtra("DataPOJO object", new Gson().toJson(list.get(position)));
startActivity(carChoiceIntent);
}
});


As you can see, I have a very long list to parse and its very inefficient to Null check all of them individually and it increases the length of the code as well.
It would be great if someone can help me with it. I am using android Volley library to communicate with the server.

Answer Source

You can use the JSONObject#opt... methods.

Example:

JSONObject singleItem = jArray.getJSONObject(i);
DataPOJO data = new DataPOJO();
data.zipcode = singleItem.optInt("zipcode", 12345);

In this case, 12345 will be a fallback in case the value mapped by "zipcode" is null.

See the docs for more information

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download