猿问

在json中找到具有不同类型值的相同键时,gson解析错误

从 API(由 Jackson 生成)获取大型 JSON 响应,并且该对象仅在响应中第一次出现时才具有其值。第二次当相同的对象(具有相同的键)到达时,它被一些数字/id 替换。


JSON 响应看起来像


[{id:1,Person:{name:"xyz",age:30}},

{id:2,Person:1}]

现在我想通过遵循 GSON 代码将整个响应存储在列表中


Type listType = new TypeToken<List<MainModel>>() {}.getType();

yourMainList = new Gson().fromJson(myres, listType);

//here myres is whole response and MainModel is my pojo class mapped with JSON response keys

但是当带有 id/number 的重复键第二次到达响应时,它给了我以下错误。


IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 11935 列路径 $[1] 处为 NUMBER。在 com.google.gson.stream.JsonReader.beginObject


有没有办法在android中通过GSON解析这样的JSON?


阿晨1998
浏览 240回答 2
2回答

手掌心

一种选择是JsonDeserializer对有问题的字段使用自定义,它可能是这样的:public class PersonDeSerializer implements JsonDeserializer<Person> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Person deserialize(JsonElement json, Type typeOfT,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonDeserializationContext context)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws JsonParseException {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return context.deserialize(json, typeOfT);&nbsp; &nbsp; &nbsp; &nbsp; } catch (JsonSyntaxException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return null to elave null or make whatever kind of a person&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后您可以设置使用该适配器反序列化的有问题的字段,例如:public class MainModel {&nbsp; &nbsp; private Long id;&nbsp; &nbsp; @JsonAdapter(PersonDeSerializer.class)&nbsp; &nbsp; private Person Person;&nbsp; // should be person, but is Person in JSON}

宝慕林4294392

您需要决定如何处理 Person 类的不同变体。这是使用com.google.gson.JsonParser执行此操作的最简单方法。private void start() {&nbsp; &nbsp; String json = "[{id:1,Person:{name:\"xyz\",age:30}},\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "{id:2,Person:1}]";&nbsp; &nbsp; HashMap<Integer, Person> map = new HashMap<>();&nbsp; &nbsp; JsonArray items = new JsonParser().parse(json).getAsJsonArray();&nbsp; &nbsp; items.forEach(jsonElement -> {&nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonObject = jsonElement.getAsJsonObject();&nbsp; &nbsp; &nbsp; &nbsp; int id = jsonObject.get("id").getAsInt();&nbsp; &nbsp; &nbsp; &nbsp; JsonElement element = jsonObject.get("Person");&nbsp; &nbsp; &nbsp; &nbsp; if(element.isJsonObject()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject personObject = element.getAsJsonObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Person person = new Person();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person.name = personObject.get("name").getAsString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person.age = personObject.get("age").getAsInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(id, person);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int personValue = element.getAsInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(id, new Person(personValue));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; for (int id : map.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; Person person = map.get(id);&nbsp; &nbsp; &nbsp; &nbsp; Log.d("myTag", id + ", name: " + person.name + ", age: " + person.age);&nbsp; &nbsp; }}class Person{&nbsp; &nbsp; String name;&nbsp; &nbsp; int age;&nbsp; &nbsp; Person(){}&nbsp; &nbsp; Person(int personValue) {&nbsp; &nbsp; &nbsp; &nbsp; // decide how the object should look&nbsp; &nbsp; }}输出:1、姓名:xyz、年龄:302、姓名:空、年龄:0如果您喜欢以更通用的方式工作而不用硬输入您的字段名称,您可以使用Gson 自定义反序列化
随时随地看视频慕课网APP

相关分类

Java
我要回答