如何从android中的json文件中获取特定记录?

时间:2012-07-20 07:09:36

标签: android json

我的SD卡中有一个包含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个对象,我不想循环。

由于

2 个答案:

答案 0 :(得分:0)

如果有1000条记录,那么请改用sqlite。 Json并不是数据库结构。

答案 1 :(得分: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
  • 的数据持有者类

找到课程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列表
  • 进行了排序
  • 然后使用binarySearch类的Collections来查找search-key-country
  • 的索引
  • 现在只使用返回的index来检索您的search-result-country对象