FlexJSON不会完全序列化类对象

时间:2010-10-08 04:44:55

标签: java json flexjson

我是FlexJson的新手,并且正在关注http://flexjson.sourceforge.net/的简单教程 我写了一个简单的程序,但似乎没有序列化对象属性。如果有人知道这个,请帮帮我

package com.webapp.enter;

import flexjson.JSONSerializer;

class PObject {

       String name;
       int age;
       String country;

        public PObject (String n, int a , String c){
            this.name = n;
            this.country = c;
            this.age = a;
        }

        public String toString(){
            return this.name + this.age + this.country;
        }

        public String[] getData(){
            return new String[]{ this.name, this.country};
        }
}

public class Person{

    public static void main(String main[]){
        PObject person = new PObject("harit",23,"india");
        System.out.println(person.name +  " - " + person.age + " - " + person.country);

        JSONSerializer serializer = new JSONSerializer();
        String out = serializer.serialize(person);
        System.out.println("S : " + out);

    }
}

输出:

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
harit - 23 - india
S : {"class":"com.webapp.enter.PObject"}
BUILD SUCCESSFUL (total time: 0 seconds)

(已删除答案更新):

我尝试使用getter / setter方法修改代码,现在它没有说出以下内容。如果我做错了,我道歉,我是新手

package com.webapp.enter;

import flexjson.JSONSerializer;

class PObject {

       String name;
       int age;
       String country;

        public PObject (){

        }

        public void setName(String name){
            this.name = name;
        }

        public void setAge(int age){
            this.age = age;
        }

        public void setCountry(String country){
            this.country = country;
        }

        public String getName(){
            return this.name;
        }

        public int getAge(){
            return this.age;
        }

        public String getCountry(){
            return this.country;
        }

}

public class Person{

    public static void main(String main[]){
        PObject person = new PObject();
        person.setAge(23);
        person.setCountry("usa");
        person.setName("test");
        System.out.println(person.name +  " - " + person.age + " - " + person.country);

        JSONSerializer serializer = new JSONSerializer();
        String out = serializer.serialize(person);
        System.out.println("S : " + out);

    }
}

输出:

test - 23 - usa
Exception in thread "main" flexjson.JSONException: Error trying to deepSerialize
        at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:97)
        at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
        at flexjson.JSONContext.transform(JSONContext.java:75)
        at flexjson.JSONSerializer.serialize(JSONSerializer.java:378)
        at flexjson.JSONSerializer.deepSerialize(JSONSerializer.java:301)
        at com.webapp.enter.Person.main(Person.java:60)
Caused by: java.lang.IllegalAccessException: Class flexjson.transformer.ObjectTransformer can not access a member of class com.webapp.enter.PObject with modifiers "public"
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
        at java.lang.reflect.Method.invoke(Method.java:607)
        at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:45)
        ... 5 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

2 个答案:

答案 0 :(得分:9)

Flexjson使用Java Bean,PObject不遵循Java Bean规范。您需要为您的属性添加getter:名称,年龄和国家/地区,或者您需要将这些字段标记为public。任何一个都可以工作。如果您打算使用JSONDeserializer来反序列化对象,请添加setter。

答案 1 :(得分:0)

如果您使用的是Spring 3或更高版本,则必须删除

之类的条目

map.remove( “org.springframework.validation.BindingResult.string”);

从序列化之前的模型映射。它就像一个魅力。最好在实现视图的视图组件中执行此操作。 看看下面的代码。

public class JsonView implements View{

@Override
public String getContentType() {
    return "application/json";
}

@Override
public void render(Map<String, ?> map, HttpServletRequest hsr, HttpServletResponse response) throws Exception {
    map.remove("org.springframework.validation.BindingResult.string");
    final String data = new JSONSerializer().deepSerialize(map);

    response.getWriter().write(data);
}

}