2017-04-12 62 views
-2

我試着去創造JsonObcject使用下面的代碼創建的JSONObject放不工作,空

try { 
      JSONObject object = new JSONObject(); 

      object.put("count", 39); 

      JSONArray results = new JSONArray(); 
      JSONObject resultsAll = new JSONObject(); 

      resultsAll.put("id", 2); 
      resultsAll.put("name", "PlaylistExample"); 
      resultsAll.put("owned_by", 4); 
      resultsAll.put("votes_per_day", 25); 
      resultsAll.put("is_subscribed", true); 

      results.put(resultsAll); 

      object.put("results", results); 

      JSONArray finalArray = object.getJSONArray("results"); 

      JSONObject finalObject = finalArray.getJSONObject(0); 

      result = new Results(finalObject); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

但其沒有工作,結果爲空值,用相同的JSONObject。我究竟做錯了什麼 ?

編輯。結果類與JsonProperties。這就是奇怪,我嘗試創建JsonObcject作爲測試

import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 
import com.fasterxml.jackson.annotation.JsonProperty; 

import org.json.JSONException; 
import org.json.JSONObject; 


@JsonIgnoreProperties(ignoreUnknown = true) 

public class Results { 
    @JsonProperty("id") 
    private int id; 
    @JsonProperty("name") 
    private String name; 
    @JsonProperty("owned_by") 
    private int owned_by; 
    @JsonProperty("votes_per_day") 
    private int votes_per_day; 
    @JsonProperty("tracks_to_play") 
    private int tracks_to_play; 
    @JsonProperty("is_subscribed") 
    private boolean is_subscribed; 

    public Results(JSONObject object) { 
     try { 

      this.id = object.getInt("id"); 
      this.name = object.getString("name"); 
      this.owned_by = object.getInt("owned_by"); 
      this.votes_per_day = object.getInt("votes_per_day"); 
      this.tracks_to_play = object.getInt("tracks_to_play"); 
      this.is_subscribed = object.getBoolean("is_subscribed"); 
     } catch (JSONException ex) { 
      ex.printStackTrace(); 

     } 
    } 


    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getOwned_by() { 
     return owned_by; 
    } 

    public void setOwned_by(int owned_by) { 
     this.owned_by = owned_by; 
    } 

    public int getVotes_per_day() { 
     return votes_per_day; 
    } 

    public void setVotes_per_day(int votes_per_day) { 
     this.votes_per_day = votes_per_day; 
    } 

    public int getTracks_to_play() { 
     return tracks_to_play; 
    } 

    public void setTracks_to_play(int tracks_to_play) { 
     this.tracks_to_play = tracks_to_play; 
    } 

    public boolean is_subscribed() { 
     return is_subscribed; 
    } 

    public void setIs_subscribed(boolean is_subscribed) { 
     this.is_subscribed = is_subscribed; 
    } 
} 
+1

你能告訴我們「結果」課的代碼嗎?它是你自己的班級還是一些圖書館的? –

+0

當然,只是片刻 – DKowal

+0

另外,你如何訪問'JSONObject對象'並獲得null?在上面的代碼中,這似乎是不可能的,因爲你寫'JSONObject對象=新的JSONObject();'你永遠不會重新分配它。 – aUserHimself

回答

0

問題進行模擬:我試圖讓測試 內的JSONObject沒有testCompile 'org.json:json:20140107'其不可能的。