解析JSON时如何不使用嵌套的try catch块?

时间:2015-06-30 22:17:56

标签: java android json try-catch

对于我的代码,是否有更优雅的解决方案,而不是JSONException有多个嵌套的try-catch块?

我嵌套它们的原因是因为如果解析中存在一个错误,我不希望其余的解析停止。我希望每个人彼此独立。

if (obj.has(GlobalVars.KEY_DESC)) {
        try {
            JSONObject descObj = obj.getJSONObject(GlobalVars.KEY_DESC);

            if (descObj.has(GlobalVars.KEY_COUNTRY)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getString(GlobalVars.KEY_COUNTRY));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }

            if (descObj.has(GlobalVars.KEY_CITY)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getString(GlobalVars.KEY_CITY));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }

            if (descObj.has(GlobalVars.KEY_POSTAL)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getString(GlobalVars.KEY_POSTAL));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }

            if (descObj.has(GlobalVars.KEY_STREET)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getString(GlobalVars.KEY_STREET));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }

            if (descObj.has(GlobalVars.KEY_SUBSTREET)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getString(GlobalVars.KEY_SUBSTREET));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }

            if (descObj.has(GlobalVars.KEY_YEAR)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getInt(GlobalVars.KEY_YEAR));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }

            if (descObj.has(GlobalVars.KEY_SQUARE_METERS)) {
                try {
                    description.put(GlobalVars.KEY_COUNTRY, descObj.getInt(GlobalVars.KEY_SQUARE_METERS));
                }
                catch (JSONException e) { e.printStackTrace(); }
            }
        }
        catch (JSONException e) { e.printStackTrace(); }
    }

2 个答案:

答案 0 :(得分:3)

您似乎在所有if声明中都做了类似的事情:

            try {
                description.put(GlobalVars.KEY_COUNTRY, descObj.getString(GlobalVars.KEY_COUNTRY));
            }
            catch (JSONException e) { e.printStackTrace(); }

因此,您可以将此代码移动到方法,并可以从每个if语句调用该方法。它会使您的代码更清晰

答案 1 :(得分:0)

我认为如果使用Gson lib,则不需要使用所有这些try catch,它会忽略缺少的字段并继续解析,但您可以使用以下代码注册缺少的字段:

package json;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class App {

    public static void main(String[] args) {
        Gson gson = new GsonBuilder().registerTypeAdapter(
                MyAnnotationBean.class,
                new AnnotatedDeserializer<MyAnnotationBean>()).create();

        String json = "{\"desc\":\"This is desc\",\"country\":\"this is country\"}";
        MyAnnotationBean tab = gson.fromJson(json, MyAnnotationBean.class);
        System.out.println(tab.desc);
        System.out.println(tab.country);

        json = "{\"desc\":\"This is desc\"}";
        tab = gson.fromJson(json, MyAnnotationBean.class);
        System.out.println(tab.desc);
        System.out.println(tab.country);

        json = "{\"country\":\"This is country\"}";
        tab = gson.fromJson(json, MyAnnotationBean.class);
        System.out.println(tab.desc);
        System.out.println(tab.country);
    }
}


class MyAnnotationBean {
    public String desc;
    public String country;
}

class AnnotatedDeserializer<T> implements JsonDeserializer<T> {

    public T deserialize(JsonElement je, Type type,
            JsonDeserializationContext jdc) throws JsonParseException {
        T obj = new Gson().fromJson(je, type);

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field f : fields) {
                try {
                    f.setAccessible(true);
                    if (f.get(obj) == null) {
//                       throw new JsonParseException("required json " +
//                       f.getName());

                        // add your code to know missing fields
                    }
                } catch (IllegalArgumentException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                }
        }
        return obj;

    }
}