简体   繁体   English

Android Volley JSON解析问题空检查

[英]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. 我在解析JSON对象时遇到问题,每个对象中都有22个元素,我想对所有元素进行空检查。 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. 有什么方法可以检查我的JSON对象中的任何元素是否为Null,这是一个非常漫长的过程,需要分别对22个元素中的每个元素执行此操作。 Below is the code for my JSON response. 以下是我的JSON响应的代码。

 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. 如您所见,我要解析的列表很长,而Null单独检查所有列表的效率很低,这也增加了代码的长度。 It would be great if someone can help me with it. 如果有人可以帮助我,那就太好了。 I am using android Volley library to communicate with the server. 我正在使用android Volley库与服务器通信。

You can use the JSONObject#opt... methods. 您可以使用JSONObject#opt...方法。

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. 在这种情况下,如果“邮政编码”所映射的值为空,则12345将作为后备。

See the docs for more information 请参阅文档以获取更多信息

如果不手动进行操作,我无法提出一种在JsonObject上检查空值的方法,但是我可以建议您使用Gson进行解析,因为它可以解决您所困扰的问题以及更多其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM