如何在android中从json文件中获取特定记录?
我的SD卡中有一个包含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
[
{"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
搜索关键字国家的索引
- 现在,只需使用返回的
检索您的索引
对象搜索结果国家/地区
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();
}
}