有时候遇到要一些模拟数据的时候,要么写个List<T>,要么写死.
然后数据比较多的时候可以引用本地资源文件,记录下如何使用本地json文件
<1>在java同级目录下创建"assets"资源文件夹
image.png<2>编辑json文本内容
至于json文件夹内容 就自定义了,想要什么内容写什么,对于一个做项目多了的开发者而言 json数据是熟悉到不能再熟悉的了
以下是我在淘宝一个个复制过来自己拼装的一些商品数据
{
"msg": "success",
"code": 0,
"data": [
{
"picture": "https://img.alicdn.com/imgextra/i1/1669409267/TB2BInSebwrBKNjSZPcXXXpapXa_!!1669409267.jpg_430x430q90.jpg",
"title": "Apple/苹果 iPad 2018款 9.7英寸wifi新款平板电脑 128G",
"price": "2368000",
"sales_volume": "111",
"stock": "1999",
"details": {
"picture": "https://img.alicdn.com/imgextra/i1/1669409267/TB2BInSebwrBKNjSZPcXXXpapXa_!!1669409267.jpg_430x430q90.jpg",
"title": "Apple/苹果 iPad 2018款 9.7英寸wifi新款平板电脑32G/128G",
"market_price": "3368000",
"price": "2368000",
"sales_volume": "111",
"stock": "1999",
"date": "2018-10-11 12:12:56",
"picture_list": [
"https://img.alicdn.com/imgextra/i1/1669409267/TB2Q8nZk0knBKNjSZKPXXX6OFXa_!!1669409267.jpg",
"https://img.alicdn.com/imgextra/i1/1669409267/TB2p.PVEHuWBuNjSszgXXb8jVXa_!!1669409267.jpg",
"https://img.alicdn.com/imgextra/i2/1669409267/TB2OFWPlbsrBKNjSZFpXXcXhFXa_!!1669409267.jpg"
]
}
},
{
"picture": "https://gd1.alicdn.com/imgextra/i1/205973549/O1CN011c5U6iMxHLYExjp_!!205973549.jpg",
"title": "Apple/苹果 iPhone X手机iphonex 苹果Xs国行美版 256G",
"price": "5980000",
"sales_volume": "11",
"stock": "899",
"details": {
"picture": "https://gd1.alicdn.com/imgextra/i1/205973549/O1CN011c5U6iMxHLYExjp_!!205973549.jpg",
"title": "Apple/苹果 iPhone X手机iphonex 苹果Xs国行美版 256G",
"market_price": "69800000",
"price": "59800000",
"sales_volume": "11",
"stock": "899",
"date": "2018-11-12 15:18:36",
"picture_list": [
"https://img.alicdn.com/imgextra/i4/205973549/O1CN011c5U6Kd5OpzLAZt_!!205973549.jpg",
"https://img.alicdn.com/imgextra/i4/205973549/O1CN011c5U6KuX9xXmZtf_!!205973549.jpg",
"https://img.alicdn.com/imgextra/i2/205973549/O1CN011c5U6E9Lf55YgDB_!!205973549.jpg",
"https://img.alicdn.com/imgextra/i1/205973549/O1CN011c5U6KuZ2MKvfze_!!205973549.jpg"
]
}
},
{
"picture": "https://img.alicdn.com/imgextra/i4/1917047079/TB2CRdjv5OYBuNjSsD4XXbSkFXa_!!1917047079.jpg_430x430q90.jpg",
"title": "2018新品 Apple/苹果 MacBook Pro 13.3英寸四核i5 256G 轻薄便携商务办公笔记本手提电脑 带Touch Bar",
"price": "12988000",
"sales_volume": "19",
"stock": "29",
"details": {
"picture": "https://img.alicdn.com/imgextra/i4/1917047079/TB2CRdjv5OYBuNjSsD4XXbSkFXa_!!1917047079.jpg_430x430q90.jpg",
"title": "2018新品 Apple/苹果 MacBook Pro 13.3英寸四核i5 256G 轻薄便携商务办公笔记本手提电脑 带Touch Bar",
"market_price": "15988000",
"price": "12988000",
"sales_volume": "19",
"stock": "29",
"date": "2018-11-10 13:13:36",
"picture_list": [
"https://img.alicdn.com/imgextra/i4/2616970884/O1CN011IOuahR2znatZg6_!!2616970884.jpg",
"https://img.alicdn.com/imgextra/i4/2616970884/O1CN011IOuahR2znatZg6_!!2616970884.jpg",
"https://img.alicdn.com/imgextra/i1/2616970884/O1CN011IOuaiILlzvnZTO_!!2616970884.jpg",
"https://img.alicdn.com/imgextra/i2/2616970884/O1CN011IOuahR1r8B2fLU_!!2616970884.jpg"
]
}
},
{
"picture": "https://img.alicdn.com/imgextra/i2/2616970884/O1CN01mKvZsH1IOuazfOjyW_!!2616970884.jpg_430x430q90.jpg",
"title": "Apple/苹果 MacBook Air 13.3英寸金属笔记本手提电脑 轻薄便携学生商务办公 正品国行",
"price": "6488000",
"sales_volume": "59",
"stock": "169",
"details": {
"picture": "https://img.alicdn.com/imgextra/i2/2616970884/O1CN01mKvZsH1IOuazfOjyW_!!2616970884.jpg_430x430q90.jpg",
"title": "Apple/苹果 MacBook Air 13.3英寸金属笔记本手提电脑 轻薄便携学生商务办公 正品国行",
"market_price": "9488000",
"price": "6488000",
"sales_volume": "59",
"stock": "169",
"date": "2018-11-15 13:13:36",
"picture_list": [
"https://img.alicdn.com/imgextra/i3/2616970884/O1CN011IOuZZJh3SxUVDH_!!2616970884.jpg",
"https://img.alicdn.com/imgextra/i3/2616970884/O1CN011IOuZYazfTki2dY_!!2616970884.jpg"
]
}
},
{
"picture": "https://img.alicdn.com/imgextra/i3/2838892713/O1CN011VuazciQmVxEnmr_!!2838892713.jpg_430x430q90.jpg",
"title": "Huawei/华为 Mate 20 全面屏珍珠屏超大广角徕卡三镜头旗舰官方正品智能手机mate20x",
"price": "4888000",
"sales_volume": "88",
"stock": "188",
"details": {
"picture": "https://img.alicdn.com/imgextra/i3/2838892713/O1CN011VuazciQmVxEnmr_!!2838892713.jpg_430x430q90.jpg",
"title": "Huawei/华为 Mate 20 全面屏珍珠屏超大广角徕卡三镜头旗舰官方正品智能手机mate20x",
"market_price": "8888000",
"price": "4888000",
"sales_volume": "88",
"stock": "188",
"date": "2018-11-25 18:18:28",
"picture_list": [
"https://img.alicdn.com/imgextra/i1/2838892713/O1CN011VuazssjjmupQNn_!!2838892713.jpg",
"https://img.alicdn.com/imgextra/i3/2838892713/O1CN011VuazmD56kzDlbJ_!!2838892713.jpg",
"https://img.alicdn.com/imgextra/i1/2838892713/O1CN011Vuazk7Qg12Du1c_!!2838892713.jpg",
"https://img.alicdn.com/imgextra/i1/2838892713/O1CN011Vuazk7Qg12Du1c_!!2838892713.jpg",
"https://img.alicdn.com/imgextra/i4/2838892713/O1CN011Vuazn427zHqnur_!!2838892713.jpg"
]
}
},
{
"picture": "https://img.alicdn.com/imgextra/i2/1114511827/O1CN011PMo550M6oGvhxM_!!0-item_pic.jpg_430x430q90.jpg",
"title": "华为HONOR/荣耀 荣耀magic 2智能手机官网全新正品荣耀Magic2新款学生魔术2",
"price": "3899000",
"sales_volume": "388",
"stock": "1888",
"details": {
"picture": "https://img.alicdn.com/imgextra/i2/1114511827/O1CN011PMo550M6oGvhxM_!!0-item_pic.jpg_430x430q90.jpg",
"title": "华为HONOR/荣耀 荣耀magic 2智能手机官网全新正品荣耀Magic2新款学生魔术2",
"market_price": "6899000",
"price": "3899000",
"sales_volume": "388",
"stock": "1888",
"date": "2018-11-28 19:18:58",
"picture_list": [
"https://img.alicdn.com/imgextra/i1/1114511827/O1CN01C0hC9v1PMo5XwBjTT_!!1114511827.jpg",
"https://img.alicdn.com/imgextra/i1/1114511827/O1CN01X2bJZp1PMo5Y7XOdS_!!1114511827.jpg",
"https://img.alicdn.com/imgextra/i4/1114511827/O1CN01XVNBuB1PMo5RGlwic_!!1114511827.jpg",
"https://img.alicdn.com/imgextra/i4/1114511827/O1CN01U7KKvX1PMo5n79Iau_!!1114511827.jpg",
"https://img.alicdn.com/imgextra/i3/1114511827/O1CN01CLLiPa1PMo5nzFdQF_!!1114511827.jpg"
]
}
}
]
}
不需要加任何标识,只需要标准的json数据格式就可以了
<3>引用和json解析的工具类,解析用的 Gson也是经常用到的
/**
* 访问本地json
*/
public class LocalJsonResolutionUtils {
/**
* 得到json文件中的内容
* @param context
* @param fileName
* @return
*/
public static String getJson(Context context, String fileName){
StringBuilder stringBuilder = new StringBuilder();
//获得assets资源管理器
AssetManager assetManager = context.getAssets();
//使用IO流读取json文件内容
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
assetManager.open(fileName),"utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
/**
* 将字符串转换为 对象
* @param json
* @param type
* @return
*/
public static <T> T JsonToObject(String json, Class<T> type) {
Gson gson =new Gson();
return gson.fromJson(json, type);
}
}
<4>最后在需要的地方引用,"ExchangeBean"是根据json内容写的一个模型,用于接收存放数据
//得到本地json文本内容
String fileName = "exchange_list.json"; //创建的json文件名称
String foodJson = LocalJsonResolutionUtils.getJson(getActivity(), fileName);
ExchangeBean exchangeBean = LocalJsonResolutionUtils.JsonToObject(foodJson, ExchangeBean.class);
adapter.addDataList(exchangeBean.getData());
有时间再补上demo,下面是效果图:
image.png
网友评论