2012-07-20 49 views
-1

我有一個文本文件在我的SD卡中包含json數組。舉例來說,文件是這樣的:如何從android中的json文件中獲取特定的記錄?

[ 
{"CountryID" : "1","CountryName" : "Australia"}, 
{"CountryID" : "2","CountryName" : "Japan"}, 
{"CountryID" : "3","CountryName" : "China"}, 
{"CountryID" : "4","CountryName" : "India"}, 
{"CountryID" : "5","CountryName" : "Holland"} 
] 

現在我想獲取基於國家ID的數據。防爆。我想通過ID = 2,我只得到一個對象。我可以在一個字符串變量中獲取整個文件,並遍歷每個對象以查找我的數據。但我不認爲這是最好的做法。因爲在我的真實文件中,我可能會有超過1000個不想循環的對象。

謝謝

+0

如果這是您的要求,那麼爲什麼你沒有根據國家ID作出單獨的搜索請求? – Dharmendra 2012-07-20 07:17:59

+0

@Dharmendra感謝您的評論Dharmendra。你能否詳細說明你的建議? – 2012-07-20 07:19:59

+0

從哪裏得到這個json響應? – Dharmendra 2012-07-20 07:22:43

回答

0

如果將要有1000條記錄,請改爲使用sqllite。 Json並不意味着數據庫結構。

+0

我同意你Prakash。但這是我無法幫助的要求。 – 2012-07-20 11:10:37

0

您可以使用下面的代碼示例做你的任務

Type listType = new TypeToken<List<Country>>() {}.getType(); 
    Gson gson = new Gson(); 
    String json = "[" 
      + "{\"CountryID\" : \"3\",\"CountryName\" : \"China\"}," 
      + "{\"CountryID\" : \"2\",\"CountryName\" : \"Japan\"}," 
      + "{\"CountryID\" : \"1\",\"CountryName\" : \"Australia\"}," 
      + "{\"CountryID\" : \"4\",\"CountryName\" : \"India\"}," 
      + "{\"CountryID\" : \"5\",\"CountryName\" : \"Holland\"}" 
      + "]"; 
    // parsing the JSON array 
    ArrayList<Country> countries = gson.fromJson(json, listType); 
    System.out.println(countries); 

    // sorting the country list based on CountryID. Ensure list is sorted, since we do binary search next 
    Collections.sort(countries); 
    System.out.println(countries); 

    Country searchKey = new Country(3,""); 
    // searching for country with ID 3 
    int index = Collections.binarySearch(countries, searchKey); 
    if(index < 0 || index >= countries.size()) System.out.println("Country with ID "+searchKey.getCountryID()+ " not found in results"); 
    else 
    { 
     System.out.println("Found Country with ID : " + searchKey.getCountryID()+ " @ index : " + index); 
     Country searchResult = countries.get(index); 
     System.out.println("Searched result : "+searchResult); 
    } 
  • 這裏我使用GSON解析Country列表
  • 對於這一點,我使用一個名爲Country
  • 數據holder類

尋找類Country

public class Country implements Comparable<Country> { 


    public Country(int countryID, String countryName) { 
     super(); 
     CountryID = countryID; 
     CountryName = countryName; 
    } 
    //"CountryID" : "1","CountryName" : "Australia" 

    private int CountryID; 
    private String CountryName; 
    /** 
    * Gets the countryID. 
    * 
    * @return <tt> the countryID.</tt> 
    */ 
    public int getCountryID() { 
     return CountryID; 
    } 
    /** 
    * Sets the countryID. 
    * 
    * @param countryID <tt> the countryID to set.</tt> 
    */ 
    public void setCountryID(int countryID) { 
     CountryID = countryID; 
    } 
    /** 
    * Gets the countryName. 
    * 
    * @return <tt> the countryName.</tt> 
    */ 
    public String getCountryName() { 
     return CountryName; 
    } 
    /** 
    * Sets the countryName. 
    * 
    * @param countryName <tt> the countryName to set.</tt> 
    */ 
    public void setCountryName(String countryName) { 
     CountryName = countryName; 
    } 
    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     return "Country [CountryID=" + CountryID + ", CountryName=" 
       + CountryName + "]"; 
    } 
    /* (non-Javadoc) 
    * @see java.lang.Comparable#compareTo(java.lang.Object) 
    */ 
    @Override 
    public int compareTo(Country o) { 
     // TODO Auto-generated method stub 
     return this.getCountryID()-o.getCountryID(); 
    } 


} 
  • Country類實現Comparable接口做Collections「二進制搜索
  • 成功解析後,我整理了Country列表
  • 然後用CollectionsbinarySearch找到你search-key-country
  • 指數
  • 現在只需使用返回的index來檢索您的search-result-country對象
相關問題