如何使用flexjson将json转换为java Obj?

时间:2017-02-08 02:33:10

标签: java json flexjson

是。使用flexjson将java obj转换为JSON是我的代码。测试没问题。但我不知道如何将JSON转换为java Obj。有人知道吗?感谢您的帮助。

        /**
         * convert java obj to json using flexjson 2.1.
         * 
         * @param obj
         * @return jsonStr
         */
    public static String bean2Json(Object obj) {
        JSONSerializer serilizer = new JSONSerializer();
        return serilizer.serialize(obj);
    }

     /**
      * convert json to java obj using flexjson 2.1.
      * 
      * @param jsonStr
      * @param objClass
      *
      * @return obj
      */
    public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
      // TODO
    }

1 个答案:

答案 0 :(得分:0)

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class DeserializerTest {
    public static void main(String[] args)
    {
        JSONSerializer serilizer = new JSONSerializer();
        Apple oneApple = new Apple(123);
        String appleString = serilizer.serialize(oneApple);

        // Convert Apple String to Apple object here
        Apple deserializedApple = new JSONDeserializer<Apple>().deserialize( appleString );
        System.out.println("AppleID: "+deserializedApple.appleID);
    }
}
public class Apple {
    public int appleID;

    public Apple(){}

    public Apple(int pAppleID){
        this.appleID=pAppleID;
    }
}

预期输出应为AppleID:123

希望有所帮助