json解析

2020-09-27  本文已影响0人  主音King

gson-2.8.0

api  'com.google.code.gson:gson:2.8.0'

json转bean,json中某个字段异常不影响其他字段解析,兼容方案如下:

public class GsonTypeAdapterFactory implements TypeAdapterFactory {
    private static final String TAG = "Gson测试T-";
    @Override
    public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        final TypeAdapter<T> adapter = gson.getDelegateAdapter(this, type);
        return new TypeAdapter<T>() {
            @Override
            public void write(JsonWriter out, T value) throws IOException {
                adapter.write(out, value);
            }

            /**
             * 处理格式异常数据
             * @param in 读
             * @return 返回
             * @throws IOException 异常捕获
             */
            @Override
            public T read(JsonReader in) throws IOException {
                try {
                    return adapter.read(in);
                } catch (Throwable e) {
                    Log.d(TAG, "e: "+e.toString());
                    consumeAll(in);
                    return null;
                }

            }

            private void consumeAll(JsonReader in) throws IOException {
                if (in.hasNext()) {
                    JsonToken peek = in.peek();
                    if (peek == JsonToken.STRING) {
                        in.nextString();
                    } else if (peek == JsonToken.BEGIN_ARRAY) {
                        in.beginArray();
                        consumeAll(in);
                        in.endArray();
                    } else if (peek == JsonToken.BEGIN_OBJECT) {
                        in.beginObject();
                        consumeAll(in);
                        in.endObject();
                    } else if (peek == JsonToken.END_ARRAY) {
                        in.endArray();
                    } else if (peek == JsonToken.END_OBJECT) {
                        in.endObject();
                    } else if (peek == JsonToken.NUMBER) {
                        in.nextString();
                    } else if (peek == JsonToken.BOOLEAN) {
                        in.nextBoolean();
                    } else if (peek == JsonToken.NAME) {
                        in.nextName();
                        consumeAll(in);
                    } else if (peek == JsonToken.NULL) {
                        in.nextNull();
                    }
                }
            }
        };
    }
}

测试demo

public class TestBody{
    public String name;
    public int age;
    public String enName;

    @Override
    public String toString() {
        return "TestBody{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", enName='" + enName + '\'' +
                '}';
    }
}

// 测试
test3("{\"name\":\"hello\",\"age\":\"error\",\"enName\":2}",TestBody.class);

    private <T> T test3(String json,Class<T> clazz){
        T t = null;
        try {
            Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonTypeAdapterFactory()).create();
            t = gson.fromJson(json,clazz);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (t==null){
            Log.d(TAG, "数据为:null");// 没有采用兼容方案,会整体返回null
        }else {
            Log.d(TAG, "数据为:"+t.toString());// 采用兼容方案后,只有异常json为null,其他字段正常
        }
        return t;
    }

针对

api 'com.alibaba:fastjson:1.2.73'

fastjson可以参考:https://blog.csdn.net/sbsujjbcy/article/details/50414188

上一篇 下一篇

猜你喜欢

热点阅读