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

如何在android中从json文件中获取特定记录?,android,json,Android,Json,我的SD卡中有一个包含json数组的文本文件。例如,假设文件如下所示: [ {"CountryID" : "1","CountryName" : "Australia"}, {"CountryID" : "2","CountryName" : "Japan"}, {"CountryID" : "3","CountryName" : "China"}, {"CountryID" : "4","CountryName" : "India"}, {"CountryID" : "5","CountryN

我的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多个我不想循环的对象


谢谢

如果将有1000条记录,请改用sqllite。Json并不意味着是数据库结构

您可以使用下面的代码示例来完成任务

    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);
    }
  • Country
    类实现了
    compariable
    接口来执行
    集合
    的二进制搜索
  • 成功解析后,我对
    国家列表进行了排序
  • 然后使用
    Collections
    类的
    binarySearch
    查找您的
    搜索关键字国家的索引
  • 现在,只需使用返回的
    索引
    检索您的
    搜索结果国家/地区
    对象

如果这是您的要求,那么您为什么不根据国家id单独请求搜索?@Dharmendra感谢您的评论Dharmendra。你能详细说明你的建议吗?从哪里得到这个json响应?我已经预先填充了json文件。我已将其放入sd卡。@hirensoni您能从sd卡读取文件内容吗?问题只在于JSON解析?我同意你的看法。但这是我不能帮助的要求。
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();
    }


}