【问题标题】:JSON deserializer with GSON using HashMap使用 HashMap 的带有 GSON 的 JSON 反序列化器
【发布时间】:2018-03-10 19:53:51
【问题描述】:

我正在尝试使用 GSON 反序列化 JSON 数据并帮助自己使用 HashMap<>,但是当我将 json 字符串设置为 java 类时出现错误,应用程序停止,并用原点标记该行错误:

funt = gson.fromJson(String.valueOf(R.string.json), stack.class);

请注意,我正在尝试将 json 数据放入自定义适配器中,代码如下:

1 个问题:我正确地将 json 字符串获取并设置到 java 类中?

2 问题:我正确地映射了标识符? (我使用“标识符”有类名,因为我不能为每个项目创建一个类,这就是我需要映射的原因)如果不是,你能告诉我怎么做吗?

我试图放置 json 字符串的 Java 类:

public class stack {
    private int id;
    private String name;
    private UserBean user;
    private 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 ItemsBean getItems() {
        return items;
    }

    public void setItems(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, IdentifierBean> identifier;

        public HashMap<String, IdentifierBean> getIdentifier(){
            return identifier;
        }

        public void setIdentifier(HashMap<String,IdentifierBean> identifier){
            this.identifier = identifier;
           }

        public static class IdentifierBean {
            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;}
        }
    }
}

这是 Activity 类:

public class stacker extends AppCompatActivity {
    stack funt;
    Gson gson;
    adapterstack adapter;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stacker);
        list = (ListVeew)findViewById(R.id.listss);

        gson = new Gson();
        funt = gson.fromJson(String.valueOf(R.string.json), stack.class);
        adapter = new adapterstack(stacker.this, (List<stack.ItemsBean>) funt.getItems());
        list.setAdapter(adapter);
    }
}

这是适配器:

public class adapterstack extends BaseAdapter {
    List<stack.ItemsBean> itemlist;
    Context sContext;
    public adapterstack( Context sContext, List<stack.ItemsBean> itemlist) {
        this.sContext = sContext;
        this.itemlist = itemlist;}
    @Override
    public int getCount() {return itemlist.size();}

    @Override
    public Object getItem(int i) {return itemlist.get(i);}

    @Override
    public long getItemId(int i) {return i;}

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) sContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rootView = inflater.inflate(R.layout.item_act, viewGroup, false);
        final stack.ItemsBean item = (stack.ItemsBean) getItem(i);
        TextView nombre = (TextView) rootView.findViewById(R.id.txt1);
        TextView genero = (TextView) rootView.findViewById(R.id.txt2);

        //Here i'm trying to get identifier data info from stack.class

        nombre.setText(item.getIdentifier().get(i).getId());
        genero.setText(item.getIdentifier().get(i).getStrong());
        return rootView;
    }
}

JSON:

[{
"id": 1001,
"name": "Super1",
"user": {
    "name": "The Super 1"
},
"items": {
    "987987M7812b163eryrt": {
        "id": 1,
        "strong": 456,
        "active": true,
        "sell": "te"
    },
    "90812bn120893juuh": {
        "id": 2,
        "strong": 4700,
        "active": true,
        "sell": "tt"
    },
    "981273jn19203nj123rg": {
        "id": 3,
        "strong": 3000,
        "active": true,
        "sell": "ti"
    }
}}]

【问题讨论】:

    标签: java android json hashmap gson


    【解决方案1】:

    我看到您尝试解析的 JSON 字符串表示一个数组。在这种情况下,您需要按如下方式解析 JSON。

    funt = gson.fromJson(String.valueOf(R.string.json), stack[].class);
    

    希望有帮助!

    【讨论】:

    • 我重新制作了json并删除了解决问题的[],非常感谢!
    • 很高兴知道这有帮助。
    • 完成,我是stackoverflow的新手,下次提醒自己
    • 非常感谢伙计。欢迎使用 StackOverflow。
    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多