JSON 反序列化,GSON,android,错误

JSON desererialize, GSON, android, error

我得到了自定义对象 MyList 的 ArrayList。 MyList 是 HashMap。 MyList 包含自定义对象 MyListElement 的 ArrayList(它也是 HashMap)。在 GSON 序列化之后它看起来像这样(为了更好的视觉而稍微正确):

{
        "list buyed list": [],
        "list name": "item1",
        "list quantity": "Не купленных: 0",
        "list main list": [
                        {
                        "listelement quantity": 4.0,
                        "listelement sum": 1.0,
                        "listelement name": "item 1"
                        },
                        {
                        "listelement quantity": 2.0,
                        "listelement sum": 2.0,
                        "listelement name": "item 2"
                        },
                        {
                        "listelement quantity": 4.0,
                        "listelement sum": 3.0,
                        "listelement name": "item 3"
                        }
                        ]
        },
        {
        "list buyed list": [],
        "list name": "item2",
        "list quantity": "Не купленных: 0",
        "list main list": [
                        {
                        "listelement quantity": 5.0,
                        "listelement sum": 1.0,
                        "listelement name": "item 4"
                        },
                        {
                        "listelement quantity": 4.0,
                        "listelement sum": 7.0,
                        "listelement name": "item 5"
                        },
                        {
                        "listelement quantity": 6.0,
                        "listelement sum": 3.0,
                        "listelement name": "item 6"
                        }
        ]
        }

但是如果我尝试反序列化它 - 我会收到错误消息:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.llaerto.mysecondbuylist.MyList.

我的反序列化代码:

public void gsonGetFromSerialize() {
        String json = "";
        try {
            FileReader fileReader = new FileReader(this.getFilesDir().getAbsolutePath() + "/mydb.json");
            BufferedReader reader = new BufferedReader(fileReader);
            String line;
            while ((line = reader.readLine()) != null) {
                json = json + line + "\n";
            }
            Log.d("TAG", json);
            fileReader.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Type listType = new TypeToken<ArrayList<MyList>>() {}.getType();
        list = new Gson().fromJson(json, listType);
    }

抱歉语言和虚拟问题。感谢您的帮助。

我的列表class代码:

public class MyList extends HashMap<String, Object> implements Serializable{
    public static final String LIST_NAME = "list name";
    public static final String LIST_LIST_MAIN = "list main list";
    public static final String LIST_LIST_BUYED = "list buyed list";
    public static final String LIST_QUANTITY = "list quantity";

    public MyList(String name) {
        this.put(LIST_NAME, name);
        this.put(LIST_LIST_MAIN, new ArrayList<MyListElement>());
        this.put(LIST_LIST_BUYED, new ArrayList<MyListElement>());
        this.put(LIST_QUANTITY, "Не купленных: " +  0);
    }

    @Override
    public String toString() {
        return (String) this.get(LIST_NAME);
    }

    public void getSum() {
        int i = ((ArrayList) this.get(LIST_LIST_MAIN)).size();
        this.put(LIST_QUANTITY, "Не купленных: " + i);
    }

    public String getName() {
        return this.get(LIST_NAME).toString();
    }

    public void setName(String name) {
        this.put(LIST_NAME, name);
    }

    public void toListAdd(MyListElement element) {
        ArrayList<MyListElement> list;
        list = (ArrayList<MyListElement>) this.get(LIST_LIST_MAIN);
        list.add(element);
        this.put(LIST_LIST_MAIN, list);
    }
}

MyListElement class 代码:

public class MyListElement extends HashMap<String, Object> implements Serializable {
    public static final String LIST_ELEMENT_NAME = "listelement name";
    public static final String LIST_ELEMENT_QUANTITY = "listelement quantity";
    public static final String LIST_ELEMENT_COLOR = "listelement color";
    public static final String LIST_ELEMENT_SUM = "listelement sum";

    public MyListElement(String name, float quantity, float sum) {
        this.put(LIST_ELEMENT_NAME, name);
        this.put(LIST_ELEMENT_QUANTITY, quantity);
        this.put(LIST_ELEMENT_SUM, sum);
        //this.put(LIST_ELEMENT_COLOR, color);
    }
}

抱歉,我第一次发布时没有。

我看不出使用 Map 来表示 json 有任何好处,也不明白为什么要那样使用它。恕我直言,使用 classes 会更好理解。所以请把这个答案作为一个建议。我希望将来有人会使用它。

首先您需要创建您的根 class。让我们称之为 RootItem。您的 Json 是(实际上看起来像) RootItem.

的数组
public class RootItem {

    @SerializedName("list buyed list")
    private Collection<String> listBuyedList;

    @SerializedName("list name")
    private String listName;

    @SerializedName("list quantity")
    private String listQuality;

    @SerializedName("list main list")
    private Collection<MainListItem> listMainList;

    // Getters & Setters
}

您的 RootItem class 有自己的数组。第一个看起来像一个字符串数组。最后一个是对象数组。让我们称这个对象为 MainListItem.

import com.google.gson.annotations.SerializedName;

public class MainListItem {

    @SerializedName("listelement quantity")
    private Integer listElementQuantity;

    @SerializedName("listelement sum")
    private Integer listElementSum;

    @SerializedName("listelement name")
    private String listElementName;

    // Getters & Setters
}

最后,您需要将 JSON 文件解析为 Collection<RootItem>,因为它是 RootItem 个对象的数组。

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.Collection;

public class GsonApp {

    private static final String TEST_JSON = "[\n" +
            "    {\n" +
            "        \"list buyed list\": [],\n" +
            "        \"list name\": \"item1\",\n" +
            "        \"list quantity\": \"Не купленных: 0\",\n" +
            "        \"list main list\": [\n" +
            "            {\n" +
            "                \"listelement quantity\": 4,\n" +
            "                \"listelement sum\": 1,\n" +
            "                \"listelement name\": \"item 1\"\n" +
            "            },\n" +
            "            {\n" +
            "                \"listelement quantity\": 2,\n" +
            "                \"listelement sum\": 2,\n" +
            "                \"listelement name\": \"item 2\"\n" +
            "            },\n" +
            "            {\n" +
            "                \"listelement quantity\": 4,\n" +
            "                \"listelement sum\": 3,\n" +
            "                \"listelement name\": \"item 3\"\n" +
            "            }\n" +
            "        ]\n" +
            "    },\n" +
            "    {\n" +
            "        \"list buyed list\": [],\n" +
            "        \"list name\": \"item2\",\n" +
            "        \"list quantity\": \"Не купленных: 0\",\n" +
            "        \"list main list\": [\n" +
            "            {\n" +
            "                \"listelement quantity\": 5,\n" +
            "                \"listelement sum\": 1,\n" +
            "                \"listelement name\": \"item 4\"\n" +
            "            },\n" +
            "            {\n" +
            "                \"listelement quantity\": 4,\n" +
            "                \"listelement sum\": 7,\n" +
            "                \"listelement name\": \"item 5\"\n" +
            "            },\n" +
            "            {\n" +
            "                \"listelement quantity\": 6,\n" +
            "                \"listelement sum\": 3,\n" +
            "                \"listelement name\": \"item 6\"\n" +
            "            }\n" +
            "        ]\n" +
            "    }\n" +
            "]";


    public static void main(String[] args) throws Exception {
        final Gson gson = new GsonBuilder().create();
        final Collection<RootItem> rootItems = gson.fromJson(TEST_JSON, new TypeToken<Collection<RootItem>>() {
        }.getType());
        System.out.println(rootItems);
    }
}

更新

如果你真的想处理地图。你可以这样做。

让我们再次创建一个根 class 并将其命名为 RootClass

public class RootClass implements Map<String, Object> {

    private transient Map<String, Object> inner;

    public RootClass() {
        this.inner = new HashMap<>();
    }

    public Collection<Map<String, Object>> getMainListElement(){
        final Object object = this.inner.get("list main list");
        return object == null ? null : (Collection<Map<String, Object>>)object;
    }


    // Delegated methods
    @Override
    public int size() {
        return inner.size();
    }

    @Override
    public boolean isEmpty() {
        return inner.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return inner.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return inner.containsValue(value);
    }

    @Override
    public Object get(Object key) {
        return inner.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        return inner.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return inner.remove(key);
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        inner.putAll(m);
    }

    @Override
    public void clear() {
        inner.clear();
    }

    @Override
    public Set<String> keySet() {
        return inner.keySet();
    }

    @Override
    public Collection<Object> values() {
        return inner.values();
    }

    @Override
    public Set<Entry<String, Object>> entrySet() {
        return inner.entrySet();
    }

    @Override
    public boolean equals(Object o) {
        return inner.equals(o);
    }

    @Override
    public int hashCode() {
        return inner.hashCode();
    }

    @Override
    public Object getOrDefault(Object key, Object defaultValue) {
        return inner.getOrDefault(key, defaultValue);
    }

    @Override
    public void forEach(BiConsumer<? super String, ? super Object> action) {
        inner.forEach(action);
    }

    @Override
    public void replaceAll(BiFunction<? super String, ? super Object, ?> function) {
        inner.replaceAll(function);
    }

    @Override
    public Object putIfAbsent(String key, Object value) {
        return inner.putIfAbsent(key, value);
    }

    @Override
    public boolean remove(Object key, Object value) {
        return inner.remove(key, value);
    }

    @Override
    public boolean replace(String key, Object oldValue, Object newValue) {
        return inner.replace(key, oldValue, newValue);
    }

    @Override
    public Object replace(String key, Object value) {
        return inner.replace(key, value);
    }

    @Override
    public Object computeIfAbsent(String key, Function<? super String, ?> mappingFunction) {
        return inner.computeIfAbsent(key, mappingFunction);
    }

    @Override
    public Object computeIfPresent(String key, BiFunction<? super String, ? super Object, ?> remappingFunction) {
        return inner.computeIfPresent(key, remappingFunction);
    }

    @Override
    public Object compute(String key, BiFunction<? super String, ? super Object, ?> remappingFunction) {
        return inner.compute(key, remappingFunction);
    }

    @Override
    public Object merge(String key, Object value, BiFunction<? super Object, ? super Object, ?> remappingFunction) {
        return inner.merge(key, value, remappingFunction);
    }
}

但是您的 json 仍然是一个集合,因此我们需要使用集合将数据获取到我们的地图。

public static void main(String[] args) throws Exception {
    final Gson gson = new GsonBuilder().create();
    final Collection<RootClass> items = gson.fromJson(TEST_JSON, new TypeToken<Collection<RootClass>>() {
    }.getType());
    System.out.println(items);
}