从 JSON 转换对象错误

发布于 2024-09-26 18:09:40 字数 1876 浏览 3 评论 0原文

首先,抱歉我的英语不好。

第二,我的问题。
我尝试转换为 JSON 并返回此结构:

class Revision{
    private String auth;
    private HashMap<String, List<HashMap<String, Object>>> rev;

    public String getAuth(){
        return auth;
}

    public HashMap<String, List<HashMap<String, Object>>> getRev(){
        return rev;
}

    public void setAuth(String auth){
        this.auth = auth;
}

    public void setRev(HashMap<String, List<HashMap<String, Object>>> rev){
        this.rev = (HashMap<String, List<HashMap<String, Object>>>) rev.clone();
}

    public String toString(){
        return "Auth: " + auth + ", rev: " + rev;
}
}

我使用以下代码执行此操作:

public static void main (String[] argc){
    Gson gson = new Gson();
    Revision revision = new Revision();

    HashMap<String, List<HashMap<String, Object>>> HM = new HashMap<String, List<HashMap<String, Object>>>();
    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> HMin = new HashMap<String, Object>();

    HMin.put("id", 12);
    HMin.put("type", "toster");
    list.add(HMin);
    HM.put("mark", list);

    revision.setRev(HM);
    revision.setAuth("ololo");

    String json = gson.toJson(revision);

    Revision test = new Gson().fromJson(json, Revision.class);

    System.out.println(json);
    System.out.println(revision);
    System.out.println(test);
}

最后我得到了此结果:

{"auth":"ololo","rev":{"mark":[{"id":12,"type":"toster"}]}}
Auth: ololo, rev: {mark=[{id=12, type=toster}]}
Auth: ololo, rev: {mark=[{id=java.lang.Object@1c672d0, type=java.lang.Object@19bd03e}]}

如您所见,转换后,对象类型参数不正确。
请问您能告诉我如何解决这个问题吗?

先感谢您!

First, sorry for my poor English.

Second, my problem.
I trying convert to JSON and back this structure:

class Revision{
    private String auth;
    private HashMap<String, List<HashMap<String, Object>>> rev;

    public String getAuth(){
        return auth;
}

    public HashMap<String, List<HashMap<String, Object>>> getRev(){
        return rev;
}

    public void setAuth(String auth){
        this.auth = auth;
}

    public void setRev(HashMap<String, List<HashMap<String, Object>>> rev){
        this.rev = (HashMap<String, List<HashMap<String, Object>>>) rev.clone();
}

    public String toString(){
        return "Auth: " + auth + ", rev: " + rev;
}
}

I do it with this code:

public static void main (String[] argc){
    Gson gson = new Gson();
    Revision revision = new Revision();

    HashMap<String, List<HashMap<String, Object>>> HM = new HashMap<String, List<HashMap<String, Object>>>();
    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> HMin = new HashMap<String, Object>();

    HMin.put("id", 12);
    HMin.put("type", "toster");
    list.add(HMin);
    HM.put("mark", list);

    revision.setRev(HM);
    revision.setAuth("ololo");

    String json = gson.toJson(revision);

    Revision test = new Gson().fromJson(json, Revision.class);

    System.out.println(json);
    System.out.println(revision);
    System.out.println(test);
}

In finally I get this result:

{"auth":"ololo","rev":{"mark":[{"id":12,"type":"toster"}]}}
Auth: ololo, rev: {mark=[{id=12, type=toster}]}
Auth: ololo, rev: {mark=[{id=java.lang.Object@1c672d0, type=java.lang.Object@19bd03e}]}

As you can see, after convertation, Object-type parameters incorrect.
Please, can you tell me, how I can fix this trouble?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-10-03 18:09:40

尝试一下这个方法,看看它是否有效?是的,我知道你想支持 Object 类型,但这只是为了尝试。

Gson gson = new Gson();
Revision revision = new Revision();

HashMap<String, List<HashMap<String, String>>> HM = new HashMap<String, List<HashMap<String, String>>>();
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> HMin = new HashMap<String, String>();

HMin.put("id", "12");
HMin.put("type", "toster");
list.add(HMin);
HM.put("mark", list);

revision.setRev(HM);
revision.setAuth("ololo");

String json = gson.toJson(revision);

Revision test = new Gson().fromJson(json, Revision.class);

System.out.println(json);
System.out.println(revision);
System.out.println(test);

[已编辑]

现在直接尝试此代码片段,并在 Revision 类中进行相应的更改。

Revision test = new Gson().fromJson("{\"auth\":\"ololo\",\"rev\":{\"mark\":[{\"id\":12,\"type\":13}]}}", Revision.class);
System.out.println(test);

Revision 类中的此项更改为此,

HashMap<String, List<HashMap<String, Integer>>> HM = new HashMap<String, List<HashMap<String, Integer>>>();

这是为了确保其在特定类型下正常工作。如果确实如此,我们将确保它无法以某种方式与 Obejct 类型一起使用。然后你可以在那里提交一个错误,为了他们好。如果您愿意,暂时可以切换到其他 API。您可以在此处找到一些选项。

Try this out and see if it is working? Yeah, I know you want to support Object type, but this is just for try sake.

Gson gson = new Gson();
Revision revision = new Revision();

HashMap<String, List<HashMap<String, String>>> HM = new HashMap<String, List<HashMap<String, String>>>();
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> HMin = new HashMap<String, String>();

HMin.put("id", "12");
HMin.put("type", "toster");
list.add(HMin);
HM.put("mark", list);

revision.setRev(HM);
revision.setAuth("ololo");

String json = gson.toJson(revision);

Revision test = new Gson().fromJson(json, Revision.class);

System.out.println(json);
System.out.println(revision);
System.out.println(test);

[Edited]

Now try this snippet directly, with a respective change in Revision class.

Revision test = new Gson().fromJson("{\"auth\":\"ololo\",\"rev\":{\"mark\":[{\"id\":12,\"type\":13}]}}", Revision.class);
System.out.println(test);

Change this in Revision class to this,

HashMap<String, List<HashMap<String, Integer>>> HM = new HashMap<String, List<HashMap<String, Integer>>>();

This is to make sure that its working good with specific type. If it does, we will be sure that it can't work with Obejct type somehow. Then you can file a bug there, for their good. And for the time being you can switch to some other API, if you like to. You can find few options here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文