?
?
类似这样的格式
public class GetJsonDataUtil {
public String getJson(Context context, String fileName) {
StringBuilder stringBuilder = new StringBuilder();
try {
AssetManager assetManager = context.getAssets();
BufferedReader bf = new BufferedReader(new InputStreamReader(
assetManager.open(fileName)));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
}
List<String> list = new ArrayList<>();
String JsonData = new GetJsonDataUtil().getJson(this, "emoji.json");//获取assets目录下的json文件数据
try {
JSONObject jsonObject = new JSONObject(JsonData);
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()){
//next()返回的数据是Object型,需要强制转化
String info = (String)keys.next();
//输出遍历的每一个元素
JSONArray jsonArray = jsonObject.optJSONArray(info);
for (int i = 0; i < jsonArray.length(); i++) {
String string = jsonArray.getString(i);
list.add(string);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
MyAdapter myAdapter = new MyAdapter(R.layout.item, list);
RecyclerView rv = findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
rv.setAdapter(myAdapter);
public class MyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
public MyAdapter(int layoutResId, @Nullable List<String> data) {
super(layoutResId, data);
}
@Override
protected void convert(@NotNull BaseViewHolder baseViewHolder, String s) {
SpanUtils.with((TextView) baseViewHolder.getView(R.id.tv))
.append("第一个")
.setFontSize(20,true)
.append(s)
.setFontSize(20+baseViewHolder.getAdapterPosition()%20,true)
.append("第"+baseViewHolder.getAdapterPosition()+"个")
.create();
}
}
类似的json文件引用..
省市区文件
JsonBean
public class JsonBean implements IPickerViewData {
/**
* name : 省份
* city : [{"name":"北京市","area":["东城区","西城区","崇文区","宣武区","朝阳区"]}]
*/
private String name;
private List<CityBean> city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<CityBean> getCityList() {
return city;
}
public void setCityList(List<CityBean> city) {
this.city = city;
}
// 实现 IPickerViewData 接口,
// 这个用来显示在PickerView上面的字符串,
// PickerView会通过IPickerViewData获取getPickerViewText方法显示出来。
@Override
public String getPickerViewText() {
return this.name;
}
public static class CityBean {
/**
* name : 城市
* area : ["东城区","西城区","崇文区","昌平区"]
*/
private String name;
private List<String> area;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getArea() {
return area;
}
public void setArea(List<String> area) {
this.area = area;
}
}
}
public ArrayList<JsonBean> parseData(String result) {//Gson 解析
ArrayList<JsonBean> detail = new ArrayList<>();
try {
JSONArray data = new JSONArray(result);
Gson gson = new Gson();
for (int i = 0; i < data.length(); i++) {
JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
detail.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
}
return detail;
}
String JsonData = new GetJsonDataUtil().getJson(this, "province.json");//获取assets目录下的json文件数据
ArrayList<JsonBean> jsonBean = parseData(JsonData);//用Gson 转成实体
?
for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
ArrayList<String> cityList = new ArrayList<>();//该省的城市列表(第二级)
ArrayList<ArrayList<String>> province_AreaList = new ArrayList<>();//该省的所有地区列表(第三极)
for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
String cityName = jsonBean.get(i).getCityList().get(c).getName();
cityList.add(cityName);//添加城市
ArrayList<String> city_AreaList = new ArrayList<>();//该城市的所有地区列表
//如果无地区数据,建议添加空字符串,防止数据为null 导致三个选项长度不匹配造成崩溃
/*if (jsonBean.get(i).getCityList().get(c).getArea() == null
|| jsonBean.get(i).getCityList().get(c).getArea().size() == 0) {
city_AreaList.add("");
} else {
city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
}*/
city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
province_AreaList.add(city_AreaList);//添加该省所有地区数据
}
/**
* 添加城市数据
*/
options2Items.add(cityList);
/**
* 添加地区数据
*/
options3Items.add(province_AreaList);
|