简体   繁体   English

使用GSON将JSON字符串转换为POJO,需要澄清

[英]JSON String to POJO, using GSON, clarification needed

I have a JSON file, as String : 我有一个JSON文件,如String

    String compString = "{\n" +
            "      \"Component\": {\n" +
            "        \"name\": \"Application\",\n" +
            "        \"environment\": \"QA\",\n" +
            "        \"hosts\": [\n" +
            "          \"box1\",\n" +
            "          \"box2\"\n" +
            "        ],\n" +
            "        \"directories\": [\n" +
            "          \"/path/to/dir1/\",\n" +
            "          \"/path/to/dir2/\",\n" +
            "          \"/path/to/dir1/subdir/\",\n" +
            "        ]\n" +
            "      }\n" +
            "    }";

I have a bean representing it (correct if incorrectly) 我有一个代表它的bean(如果不正确,则正确)

public class Component {

    String name;
    String environment;

    List<String> hosts = new ArrayList<String>();
    List<String> directories = new ArrayList<String>();

    // standard getters and setters
}

I am trying to feed this String to this class by: 我试图通过以下方式将此String馈给此类:

    Gson gson = new Gson();
    Component component = gson.fromJson(compString, Component.class);

    System.out.println(component.getName());

Above does not work. 以上不起作用。 (I am getting null back, as if Component's name value is never set) (我得到的返回null ,好像从未设置组件的名称值一样)

What am i missing please? 我想念什么?

In fact, you have to remove the enclosing class from Json. 实际上,您必须从Json中删除封闭的类。

Indeed, JSON begins with the content of the enclosing class. 实际上,JSON是从封闭类的内容开始的。

So your JSON would be: 因此,您的JSON将是:

 String compString = "{\n" +
                    "        \"name\": \"Application\",\n" +
                    "        \"environment\": \"QA\",\n" +
                    "        \"hosts\": [\n" +
                    "          \"box1\",\n" +
                    "          \"box2\"\n" +
                    "        ],\n" +
                    "        \"directories\": [\n" +
                    "          \"/path/to/dir1/\",\n" +
                    "          \"/path/to/dir2/\",\n" +
                    "          \"/path/to/dir1/subdir/\",\n" +
                    "        ]\n" +
                    "      }\n";
String compString = 
                "       {\n" +
                "        \"name\": \"Application\",\n" +
                "        \"environment\": \"QA\",\n" +
                "        \"hosts\": [\n" +
                "          \"box1\",\n" +
                "          \"box2\"\n" +
                "        ],\n" +
                "        \"directories\": [\n" +
                "          \"/path/to/dir1/\",\n" +
                "          \"/path/to/dir2/\",\n" +
                "          \"/path/to/dir1/subdir/\",\n" +
                "        ]}" ;

I think you should read more about json, I have removed something in your json string and then success. 我认为您应该阅读有关json的更多信息,我已经删除了json字符串中的某些内容,然后成功了。

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

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