简体   繁体   English

Gson到Pojo内部类的序列化

[英]Gson to pojo serialization of inner class

I am trying to take a json and convert it into a POJO using Gson's ability. 我正在尝试使用json并使用Gson的功能将其转换为POJO。 I got it working with the serialized class as a class in a separate package, but now I am trying to get the serialized class to be an inner class of the class which is calling it, kind of like how I have depicted below. 我将它与序列化类作为一个单独的包中的类一起使用,但是现在我试图使序列化类成为正在调用它的类的内部类,就像我在下面描述的那样。

public class A {
    private final Gson gson;

    public A() {
        gson = new GsonBuilder().serializeNulls().create();
    }

    public void foo(String json){
        B b = gson.fromJson(json, B.class);
    }

    public static class B {
         private String bar;

         public String getBar(){
             return bar;
         }
    }
}

Sample JSON: 样本JSON:

{"bar": "test"}

This setup returns null values for the inner class variables; 此设置为内部类变量返回空值; in this case, just bar. 在这种情况下,只是吧。 I've made sure the json being passed in matches the names of variables in Class B, in this example. 在此示例中,我确保传递的json与B类中的变量名称匹配。 Additionally, I have experimented around with making the inner class non-static and private with no success. 此外,我还尝试过使内部类变为非静态和私有类,但没有成功。 To my understanding it needs to be a static class in order for it to be serialized using gson. 据我了解,它必须是一个静态类,才能使用gson进行序列化。 Is there anything blatantly obvious that I am doing wrong that you can see? 您是否可以看到有任何明显的我在做错事?

The error is somewhere else. 错误在其他地方。 Most probably the String or the Gson you pass is configured the wrong way. 您传递的StringGson很可能配置错误。 I pasted the exact same code and it works fine for me: 我粘贴了完全相同的代码,它对我来说很好用:

public class Main {

    public static void main (String[] args) {
        new A().foo("{\"bar\": \"test\"}");
    }

    public static class A {
        private final Gson gson;

        public A() {
            gson = new GsonBuilder().serializeNulls().create();
        }

        public void foo(String json){
            B b = gson.fromJson(json, B.class);
            System.out.println(b);
        }

        public static class B {
            private String bar;

            public String getBar(){
                return bar;
            }

            @Override
            public String toString() {
                return "B{bar='" + bar + '\'' +
                        '}';
            }
        }
    }

}

Inner classes are supposed to work so the problem is elsewhere. 内部类应该起作用,所以问题出在其他地方。 This will print out: 这将打印出:

B{bar='test'} B {bar ='test'}

So I imagine this is a common issue, now that I've run into it twice. 所以我想这是一个常见的问题,因为我已经碰到过两次了。 The issue was in the JSON string being passed in. I copied and pasted the JSON from somewhere into the command line. 问题出在传入的JSON字符串中。我将JSON从某处复制并粘贴到了命令行中。 As a result, the quotations weren't being registered as valid quotation marks. 结果,报价没有被注册为有效的引号。 It was as opposed to " . The second is what is registered as a quotation mark. Thanks for all the help. 它是而不是" 。第二个是注册为引号的内容。感谢所有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM