2017-01-13 6 views
3

におけるオブジェクト内の一般的なオブジェクトをデシリアライズ私は、JSONとして私に送られたクラスの表現であるWebApiReturnと呼ばれるクラスを持っていますは、Java

public class MyObject { 
    public int ID_Obj; 
    public String ObjectName; 
    public Date LastLoginDate; 
} 

そして、私のWebサービスによって送信されたJSONとしてフォーマット文字列を取得し、このJavaクラスに変換するのgetObjectと呼ばれる機能::私は私のWebサービスから取得しようとしている

public Object getObject(Class wantedResponseClass) throws JSONException{ 
     Gson gson = new Gson(); 
     object = gson.fromJson(this.result, wantedResponseClass); 

     return object; 
    } 

そして、私のJSON文字列には、例えば、次のとおりです。

{"objectReturn":{"iD_Obj":123,"objectName":"TestName","lastLoginDate":"0001-01-01T00:00:00"},"hasError":false,"errorMessage":null,"errorCode":null} 

で、私のコードで私は次のように私のオブジェクトを取得しよう:

WebApiReturn<MyObject> responseFromServer = new WebApiReturn<>(); 
try { 
    responseFromServer =(WebApiReturn<MyObject>) getObject(responseFromServer.getClass()); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 

しかし、その後、一般的なT ObjectReturn、すべきであるとWebApiReturn<MyObject> responseFromServer = new WebApiReturn<>();がJsonからの表現で満たされていないと宣言したときMyObjectになっています。誰かが私は今何が間違っているのか知っていますか?別のタイプのデシリアライゼーションなどを使用する必要がありますか?

答えて

1

デシリアライザにジェネリッククラスを持つクラスを作成するには、インスタンスT (UIスレッドの非同期サーバーからJsonを取得するために)AsyncTaskを拡張し、JSONObjectを使用してクラスを2つのクラスに分けるために、クラス自体とジェネリック内部を分割します。それから私は2要素として戻ってきます。ここで

は私のクラスである:

public class RestClient<t> extends AsyncTask<String, String, String> { 

private Class<t> tClass; 

private t returnGenericObject; 
private WebApiReturn webApiReturn; 

private AsyncCallback callback; 

public RestClient(Class<t> tClass) 
{ 
    //The instance of the class you want 
    this.tClass = tClass; 
    webApiReturn= new WebApiReturn(); 
} 

//You get the WebApiReturn with this method 
public WebApiReturn getWebApiReturn(){ 
    return webApiReturn; 
} 

//You get the inside generic object with this method 
public Object getObjectResponse(){ 
    return returnGenericObject; 
} 


@Override 
protected String doInBackground(String... OnlyTheURL) { 
    String urlString = OnlyTheURL[0]; 
    try { 
     URL url = new URL(urlString); 

     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 

     connection.connect(); 

     String result = convertStreamToString(connection.getInputStream()); //A function to convert Stream to String... 

     connection.disconnect(); 

     //Created a JSONObject from the result of the connection 
     JSONObject jsonObject = new JSONObject(result); 

     //Don't set the Generic Object because it will crash when you try to get back 
     webApiReturn.setHasError(jsonObject.getBoolean("hasError")); 
     webApiReturn.setErrorMessage (jsonObject.getString("errorMessage")); 
     webApiReturn.setErrorCode(jsonObject.getString("errorCode")); 

     //Get the String of the generic object within the WebApiReturn 
     String obj = jsonObject.get("objetoRetorno").toString(); 
     //With Gson convert it to the class you expect (that you instanciated in the constructor) 
     Gson gson = new Gson(); 
     ObjetoDeRetorno = gson.fromJson(obj, tClass); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

とあなたはちょうどこのように行い、それを呼びたい:

RestClient<YourObject> restClient = new RestClient<>(YourObject.class); 
restClient.execute(); 
Thread.sleep(5000); //You can do this other better ways, but just as example I'm doing this... 

//After the execution you get your objects: 
WebApiReturn return = getWebApiReturn(); 
YourObject object = (YourObject) restClient.getObjectResponse(); 
0

あなたはTのランタイムインスタンスはしたがって、あなたがMyObjectObjectの代わりに直列化復元しようとしているObjectに置き換えますで、ジェネリック型を使用することはできません。

TMyObjectに置き換えた場合、少なくともあなたの例ではうまくいくはずです。しかし、ObjectReturnは複数の型を持つことができると想像しています。ここで答えが得られるかもしれません。Gson deserialize JSON array with multiple object types