2015-02-23 105 views
0

我遇到一個問題轉換JSON來列出<>,我已經嘗試了不同的解決方案,但沒有任何線索GSON - 從Json的轉換爲List <>

的json結果如下:

{ 
    "Return":0, 
    "List":[ 
     { 
     "Code":524288, 
     "Label":"TEST" 
     }, 
     { 
     "Code":524289, 
     "Label":"TEST1" 
     }, 
     { 
     "Code":524290, 
     "Label":"TEST2" 
     } 
    ] 
} 

我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    List<Questionaire> qstlist = new ArrayList<Questionaire>(); 
    try { 
     result = new RequestTask().execute("http:/dd.com/categories/current?owner=ccc").get(); 
     json = new JSONObject(result); 

    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    GsonBuilder gsonb = new GsonBuilder(); 
    Gson gson = gsonb.create(); 
    Type listType = new TypeToken<List<Questionaire>>(){}.getType(); 
    qstlist = (List<Questionaire>) gson.fromJson(result, listType); 
    Questionaire qst = null; 
    qst = gson.fromJson(result, Questionaire.class); 

    Toast.makeText(MainActivity.this, "Result "+qstlist.size(), Toast.LENGTH_SHORT).show(); 
} 

class RequestTask extends AsyncTask<String, String, String>{ 

    @Override 
    protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String responseString = null; 
     try { 
      response = httpclient.execute(new HttpGet(uri[0])); 
      StatusLine statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       responseString = out.toString(); 
       out.close(); 
      } else{ 
       //Closes the connection. 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 
      //TODO Handle problems.. 
     } catch (IOException e) { 
      //TODO Handle problems.. 
     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
    } 
} 

我的問卷調查類:

public class Questionaire { 
    String Code; 
    String Label; 
    public String getCode() { 
     return Code; 
    } 
    public void setCode(String Code) { 
     this.Code = Code; 
    } 
    public String getLabel() { 
     return Label; 
    } 
    public void setLabel(String Label) { 
     this.Label = Label; 
    } 
} 

這裏的一切,我不能看到有關

回答

0

在這個JSON,你得到{代碼,標籤}對象列表什麼是錯的,但在一個對象的List財產。

您首先需要將此列表封裝在另一個對象中。 如:

public class QuestionaireList { 
    public List<Questionaire> List; 
} 

List<Questionaire> qsts = gson.fromJson(result, QuestionaireList.class).List; 
+0

我應該創建一個新類嗎?我應該改變我的代碼? – 2015-02-23 14:29:03

+0

是的,一個班級,我忘了關鍵字 – Gorcyn 2015-02-23 14:29:58

+0

謝謝它的工作原理 – 2015-02-23 14:32:34

1

需要使用另一個類映射全部結果,那麼

public class MyJson { 
    @SerializedName("List") 
    private List<Questionaire> list; 

    @SerializedName("Return") 
    private Integer return1; 

    //... getters and setters 
} 

你的類型需要綁定到「MyJson」或任何你的名字的類。

+0

您能否給我提供更多詳細信息,我不明白 – 2015-02-23 14:29:34

+0

我對您的JSON進行了格式化,主要是爲了強調您獲得一個包含2個屬性的對象,列表「,並且在這個'List'屬性中你可以得到你感興趣的對象列表。所以你必須爲Gson提供這個對象的層次結構,以便它可以理解結構。 – Gorcyn 2015-02-23 14:33:17

+0

您需要創建我提供的示例類,然後執行類似於此的gson.fromJson(result,MyJson.class); @serilizedName註釋將json屬性名稱綁定到java propery。返回是一個關鍵字,否則將無法工作 – 2015-02-23 14:40:03

0

我假設它是你想映射到'qstList'的List部分。如果是這樣,那麼你需要首先從json的其餘部分提取它:

Gson gson = new Gson(); 
JsonObject resultObj = gson.fromJson(result, JsonObject.class); 
JsonArray jsonList = resultObj.get("List").getAsJsonArray(); 
Type listType = new TypeToken<List<Questionaire>>(){}.getType(); 
qstList = gson.fromJson(jsonList, listType); 
相關問題