Json GSON未正确初始化静态列表
如果我这样做:Json GSON未正确初始化静态列表,json,arraylist,gson,Json,Arraylist,Gson,如果我这样做: public static volatile ArrayList<Process> processes = new ArrayList<Process>(){ { add(new Process("News Workflow", "This is the workflow for the news segment", "image")); } }; public static volatile ArrayList<P
public static volatile ArrayList<Process> processes = new ArrayList<Process>(){
{
add(new Process("News Workflow", "This is the workflow for the news segment", "image"));
}
};
public static volatile ArrayList<Process> processes = new ArrayList<Process>();
processes.add(new Process("nam", "description", "image"));
String jsonResponse = gson.toJson(processes);
jsonResponse为空
但如果我这样做:
public static volatile ArrayList<Process> processes = new ArrayList<Process>(){
{
add(new Process("News Workflow", "This is the workflow for the news segment", "image"));
}
};
public static volatile ArrayList<Process> processes = new ArrayList<Process>();
processes.add(new Process("nam", "description", "image"));
String jsonResponse = gson.toJson(processes);
为什么会这样?我不知道Gson有什么问题,但是您知道吗,您正在这里创建ArrayList的子类
newarraylist(){
{
添加(新流程(“新闻工作流”,“这是新闻片段的工作流”,“图像”));
}
};
你可以通过电话查一下
System.out.println( processes.getClass().getName() );
它不会打印java.util.ArrayList
我认为您希望使用静态初始化作为
publicstaticvolatilearraylist进程=newarraylist();
静止的{
添加(新流程(“新闻工作流”,“这是新闻片段的工作流”,“图像”));
};
似乎匿名类存在问题,同样的问题也存在
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GSonAnonymTest {
interface Holder {
String get();
}
static Holder h = new Holder() {
String s = "value";
@Override
public String get() {
return s;
}
};
public static void main( final String[] args ) {
final GsonBuilder gb = new GsonBuilder();
final Gson gson = gb.create();
System.out.println( "h:" + gson.toJson( h ) );
System.out.println( h.get() );
}
}
UPD:最后一点“…匿名类和本地类被忽略,不包括在序列化或反序列化中…”