【问题标题】:how to convert json to java Obj using flexjson?如何使用 flexjson 将 json 转换为 java Obj?
【发布时间】:2017-02-08 02:33:10
【问题描述】:

是的。使用 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
    }

【问题讨论】:

  • public static T json2Bean(String jsonStr, Class objClass) { JSONDeserializer js = new JSONDeserializer();返回 js.deserialize(jsonStr, objClass); }

标签: java json flexjson


【解决方案1】:
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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    相关资源
    最近更新 更多