问题描述
我有一个来自外部 API 的 json 对象,它具有以下形式的对象“列表”:
constraints {
implementation('androidx.fragment:fragment:1.3.1') {
because 'avoid bug'
}
implementation('androidx.fragment:fragment-ktx:1.3.1') {
because 'avoid bug'
}
}
我的解串器和 POJO 看起来像这样:
{
"width": 10,"height": 20
"pointData": {
"0": {
"x": 7,"y": 5,...
},"1": {
"x": 4,"y": 20,"2": {
"x": 1,"y": 3,...
}
}
由于 class KeyedListDeserializer<T> implements JsonDeserializer<List<T>> {
@Override
public List<T> deserialize(JsonElement json,Type typeOfT,JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
List<T> things = new ArrayList<>();
int key = 0;
while (jsonObject.has(String.valueOf(key))) {
JsonElement element = jsonObject.get(String.valueOf(key));
T value = context.deserialize(element,new Typetoken<T>(){}.getType());
things.add(value);
key += 1;
}
return things;
}
}
class PointDataKeyedList extends KeyedListDeserializer<PointData> {}
class Canvas {
int width;
int height;
@JsonAdapter(PointDataKeyedList.class)
List<PointData> pointData;
...
}
class PointData {
int x;
int y;
...
}
对象的所有键都只是索引,我认为我可以将其反序列化为列表而不是映射。运行时,Gson 成功通过反序列化,但是当我尝试访问点数据时,我得到:
pointData
我不确定这里出了什么问题。我读过一些关于类型擦除的文章,这是我在搜索这个问题时听到的很多内容,但我不是很理解。
解决方法
在 Gson 中很容易。
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
url = 'https://www.target.com/p/prairie-farms-vitamin-d-milk-1gal/-/A-47103206#lnk=sametab'
driver.get(url)
driver.implicitly_wait(5)
elements = driver.find_elements_by_xpath("//a[contains(@class,'TabHeader__Styled')]")
for el in elements:
ActionChains(driver).move_to_element(el).perform()
el.click()
driver.quit()
上面的类型适配器工厂做了以下事情:
- 检查目标类型是否为
public final class MapToListTypeAdapterFactory implements TypeAdapterFactory { private MapToListTypeAdapterFactory() { } @Override @Nullable public <T> TypeAdapter<T> create(final Gson gson,final TypeToken<T> typeToken) { if ( !List.class.isAssignableFrom(typeToken.getRawType()) ) { return null; } final Type elementType = typeToken.getType() instanceof ParameterizedType ? ((ParameterizedType) typeToken.getType()).getActualTypeArguments()[0] : Object.class; final TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType)); final TypeAdapter<List<Object>> listTypeAdapter = new TypeAdapter<List<Object>>() { @Override public void write(final JsonWriter out,final List<Object> value) { throw new UnsupportedOperationException(); } @Override public List<Object> read(final JsonReader in) throws IOException { in.beginObject(); final ArrayList<Object> list = new ArrayList<>(); while ( in.hasNext() ) { final int index = Integer.parseInt(in.nextName()); final Object element = elementTypeAdapter.read(in); ensureSize(list,index + 1); list.set(index,element); } in.endObject(); return list; } }; @SuppressWarnings("unchecked") final TypeAdapter<T> typeAdapter = (TypeAdapter<T>) listTypeAdapter .nullSafe(); return typeAdapter; } // https://stackoverflow.com/questions/7688151/java-arraylist-ensurecapacity-not-working/7688171#7688171 private static void ensureSize(final ArrayList<?> list,final int size) { list.ensureCapacity(size); while ( list.size() < size ) { list.add(null); } } }
(它实际上不适用于链表,但可以简化); - 提取目标列表的类型参数,因此其元素类型并解析相应的类型适配器;
- 用地图读取适配器替换原始类型适配器,该适配器读取地图键并将它们假定为新列表索引(并在必要时扩大结果列表)并使用原始元素类型适配器读取每个地图值。请注意,列表可能具有稀疏索引的假设是易受攻击的,我仅将其用于演示目的(例如,输入 JSON 声明了一个单元素映射,其中唯一的索引“999999”可显着扩大列表),因此您可以仅使用
List
方法忽略index
值。
只需使用 add(element)
注释 pointData
类中的 Canvas
字段,它就会起作用。
我担心你的 POJO 类需要像
class Canvas {
int width;
int height;
Map<String,PointData> pointData;
...
}
class PointData {
int x;
int y;
...
}
你可以去掉 PointDataKeyedList 和 KeyedListDeserializer
public static void main(String[] args) {
String json = "{\n" +
" \"width\": 10,\n" +
" \"height\": 20,\n" +
" \"pointData\": {\n" +
" \"0\": {\n" +
" \"x\": 7,\n" +
" \"y\": 5\n" +
" },\n" +
" \"1\": {\n" +
" \"x\": 4,\n" +
" \"y\": 20\n" +
" },\n" +
" \"2\": {\n" +
" \"x\": 1,\n" +
" \"y\": 3\n" +
" }\n" +
" }\n" +
"}";
System.out.println(json);
Gson gson = new Gson();
Canvas canvas = gson.fromJson(json,Canvas.class);
System.out.println(canvas);
}