2017-02-08 4 views
0

はい。これは私のコードはJSONにflexjsonを使用してJavaのobjを変換するものです。テストで大丈夫です。しかし、私はJSONをjava Objに変換する方法を知らない。誰も知っている?あなたの助けを前もってありがとう。flexjsonを使用して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 
    } 
+0

パブリック静的 T json2Bean(文字列jsonStr、クラス objClass){ \tを助けるホープ\t JSONDeserializer js = new JSONDeserializer (); \t \t return js.deserialize(jsonStr、objClass); \t} –

答えて

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

は、

+0

です。どうもありがとうございます。 –

関連する問題