Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用Gson在HashMap中存储JSON_Android_Json_Hashmap - Fatal编程技术网

Android 如何使用Gson在HashMap中存储JSON

Android 如何使用Gson在HashMap中存储JSON,android,json,hashmap,Android,Json,Hashmap,团队我需要一种正确的方法来使用Gson将数据存储到HashMap中,因为我正在使用follow Java类,但是当我尝试从HashMap中获取一些值时,我只得到NullPointerException错误,当我尝试从HashMap中获取一些值时,我成功地将Json获取到Java类中,为什么我需要将它存储在HashMap中,因为对象名称非常随机,并且不能为每个项目创建类 我尝试在其中存储JSON数据的Java类: public class Response { private int id; p

团队我需要一种正确的方法来使用Gson将数据存储到
HashMap
中,因为我正在使用follow Java类,但是当我尝试从
HashMap
中获取一些值时,我只得到
NullPointerException
错误,当我尝试从
HashMap
中获取一些值时,我成功地将Json获取到Java类中,为什么我需要将它存储在
HashMap
中,因为对象名称非常随机,并且不能为每个项目创建类

我尝试在其中存储JSON数据的Java类:

public class Response {

private int id;
private String name;
private UserBean user;
private List<ItemsBean> items;

public int getId() {return id;}

public void setId(int id) {this.id = id;}

public String getName() {return name;}

public void setName(String name) {this.name = name;}

public UserBean getUser() {return user;}

public void setUser(UserBean user) {this.user = user;}

public List<ItemsBean> getItems() {return items;}

public void setItems(List<ItemsBean> items) {this.items = items;}


public static class UserBean {

    private String name;

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

}


public static class ItemsBean {

    private HashMap<String, CodecBean> codec;

    public HashMap<String, CodecBean> getCodec() {return codec;}

    public void setCodec(HashMap<String, CodecBean> codec) {this.codec = codec;}

    public static class CodecBean {

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {return id;}

        public void setId(int id) {this.id = id;}

        public int getStrong() {return strong;}

        public void setStrong(int strong) {this.strong = strong;}

        public boolean isActive() {return active;}

        public void setActive(boolean active) {this.active = active;}

        public String getSell() {return sell;}

        public void setSell(String sell) {this.sell = sell;}
    }
}}
我尝试通过以下方式获取值:

Response.ItemsBean item = (Response.ItemsBean) getItem(i);
TextView txt;
txt.setText(item.getCodec().get(i).getSell());
你能告诉我我做错了什么吗


提前感谢

我认为您需要使用LinkedTreeMap而不是Hashmap 更改跟随代码

....
private LinkedTreeMap<String, CodecBean> codec;
...
。。。。
专用LinkedTreeMap编解码器;
...

您已被标记。。。这会很好…你想保存数据吗?@Nasimxon是的,我需要将“项目”保存在HashMap@Selvin为什么这么说?可能是重复的:我创建了这样的类,结果相同,
NullPointerException
....
private LinkedTreeMap<String, CodecBean> codec;
...