简单new Gson,复杂格式用new GsonBuilder().registerTypeAdapter()解析,
常见错误:
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 3 column 17 path $.authors
修改方案:
static class GsonError1Deserializer implements JsonDeserializer { @Override public GsonError1 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); final JsonElement jsonTitle = jsonObject.get("name"); final String name = jsonTitle.getAsString(); JsonElement jsonAuthors = jsonObject.get("authors"); GsonError1 gsonError1 = new GsonError1(); if (jsonAuthors.isJsonArray()) {//如果数组类型,此种情况是我们需要的 //关于context在文章最后有简单说明 GsonError1.AuthorsBean[] authors = context.deserialize(jsonAuthors, GsonError1.AuthorsBean[].class); gsonError1.setAuthors(Arrays.asList(authors)); } else {//此种情况为无效情况 gsonError1.setAuthors(null); } gsonError1.setName(name); return gsonError1; } } static class AuthorDeserializer implements JsonDeserializer { @Override public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); final GsonError1.AuthorsBean author = new GsonError1.AuthorsBean(); if (jsonObject.get("id") == null && jsonObject.get("name") == null) { author.setId(null); author.setName(null); } else if (jsonObject.get("id") != null && jsonObject.get("name") == null){ author.setId(jsonObject.get("id").getAsString()); author.setName(null); } else if (jsonObject.get("id") == null && jsonObject.get("name") != null){ author.setId(null); author.setName(jsonObject.get("name").getAsString()); } else { author.setId(jsonObject.get("id").getAsString()); author.setName(jsonObject.get("name").getAsString()); } return author; } } public static void test3() { //TODO: String json1 = "{ " + " "name": "java", " + " "authors": "" " + "}"; String json = "{ " + " "name": "java", " + " "authors": [ " + " { " + " "id": "1", " + " "name": "Joshua Bloch" " + " }, " + " { " + " "id": "2" " + " } " + " ] " + "}"; GsonBuilder gsonBuilder = new GsonBuilder(); //注册TypeAdapter gsonBuilder.registerTypeAdapter(GsonError1.class, new GsonError1Deserializer()); gsonBuilder.registerTypeAdapter(GsonError1.AuthorsBean.class, new AuthorDeserializer()); Gson gson = gsonBuilder.create(); GsonError1 gsonError1 = gson.fromJson(json, GsonError1.class); System.out.println(gsonError1); }
File file = new File(path + "/app/src/main/java/com/anny/protobuf/json/json.json"); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line; StringBuffer sb = new StringBuffer(); while ((line = br.readLine()) != null){ sb.append(line); } fis.close(); isr.close(); br.close(); System.out.println(sb.toString());
//Anny 没有头的特殊数据解析 Gson gson = new Gson(); JsonParser jsonParser = new JsonParser(); JsonArray jsonArray = (jsonParser).parse(sb.toString()).getAsJsonArray(); ArrayList<GsonBean> gsonBeans = new ArrayList<>(); for (JsonElement jsonElement: jsonArray) { GsonBean gsonBean = gson.fromJson(jsonElement, GsonBean.class); gsonBeans.add(gsonBean); } System.out.println(gsonBeans);