0

我正在尝试序列化这个内部类

static class functionMessage{
    String type = "function";
    String id;
    String function;
    Object parameters;

    public functionMessage(String ID, String Function, Boolean Parameters) {
        this.id = ID;
        this.function = Function;
        this.parameters = (Boolean) Parameters;
    }
}

new flexjson.JSONSerializer().exclude("*.class").serialize( 
    new functionMessage(
        "container", 
        "showContainer", 
        Boolean.TRUE
    ) 
) 

但只是{}被退回。

如果我尝试public在每个成员变量之前添加,则会出现此错误:

flexjson.JSONException: Error trying to deepSerialize  
Caused by: java.lang.IllegalAccessException: Class flexjson.BeanProperty can not access a member of class with modifiers "public"

我尝试按照示例进行操作,但它没有显示如何Person构造,并且我不知道将类static设为内部类会如何影响这一点,因为我对 Java 还是很陌生。

我还尝试阅读 Google 针对该错误提供的所有解决方案,但仍然一无所获。

如何返回一个内部类flexjson.JSONSerializer()的所有成员变量?static

4

1 回答 1

2

但只返回 {}。

显然flexjson使用 getter 来解析 JSON 元素。你的班级没有。

只需添加相应的getter

public String getType() {
    return type;
}

public String getId() {
    return id;
}

public String getFunction() {
    return function;
}

public Object getParameters() {
    return parameters;
}

至于

如果我尝试在每个成员变量之前添加 public ,则会出现此错误:

那是因为您的static类具有默认的包访问权限,因此它的字段对于其声明的包之外的任何类都不可见。

于 2014-01-11T20:03:33.390 回答