我有一個JSON
文件,爲String
:JSON字符串POJO,使用GSON,澄清需要
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" +
" }";
我代表它的一個bean(正確的,如果不正確地)
public class Component {
String name;
String environment;
List<String> hosts = new ArrayList<String>();
List<String> directories = new ArrayList<String>();
// standard getters and setters
}
我想通過以下方式將此字符串提供給此類:
Gson gson = new Gson();
Component component = gson.fromJson(compString, Component.class);
System.out.println(component.getName());
上述操作不起作用。 (我得到null
回來,好像組件的名字值從未設置)
我在想什麼?
您是否收到錯誤信息,或空豆? –
在這一點上,我越來越'null'好像豆從未初始化 – JAM