Android获取assets文件夹下的json数据,并解析!
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
以前看过这些知识,今天用到的时候,发现自己忘记了,都不知道assets文件夹在哪个目录下。百度了一下,assets目录和java目录是同一个级别的。
我们右击assets目录新建File,起名"dynamic.json",然后我们在文件中写一些json数据:
{"dynamic":[
{
"username":"燕",
"createTime":"04-23 13:12",
"commentNum":"45",
"content":"喔喔喔",
"headIcon":"http://img2.imgtn.bdimg.com/it/u=1576185143,2361770572&fm=214&gp=0.jpg",
"images":"http://img4.imgtn.bdimg.com/it/u=3776739438,757564394&fm=214&gp=0.jpg",
"level":"公告",
"priseNum":"23"
},
{
"username":"徐金杰",
"createTime":"04-23 13:12",
"commentNum":"45",
"content":"哩哩里",
"headIcon":"http://img4.duitang.com/uploads/item/201508/17/20150817142843_4smWc.jpeg",
"images":"http://pic.58pic.com/58pic/13/70/90/29358PICQjG_1024.jpg",
"level":"公告",
"priseNum":"43"
},{
"username":"宋",
"createTime":"04-23 13:12",
"commentNum":"23",
"content":"咕咕咕",
"headIcon":"http://img2.imgtn.bdimg.com/it/u=1576185143,2361770572&fm=214&gp=0.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"重要",
"priseNum":"12"
},
{
"username":"唐",
"createTime":"04-23 13:12",
"commentNum":"450",
"content":"喵喵喵",
"headIcon":"http://img4.duitang.com/uploads/item/201508/17/20150817142843_4smWc.jpeg",
"images":"http://img4.imgtn.bdimg.com/it/u=3776739438,757564394&fm=214&gp=0.jpg",
"level":"公告",
"priseNum":"345"
},{
"username":"米",
"createTime":"04-23 13:12",
"commentNum":"333",
"content":"咪咪咪",
"headIcon":"http://www.haopic.me/wp-content/uploads/2015/12/2015122808171644.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"重要",
"priseNum":"323"
},{
"username":"冯",
"createTime":"04-23 13:12",
"commentNum":"322",
"content":"么么哒",
"headIcon":"http://img.bitscn.com/upimg/allimg/c160120/1453262U560940-A5c.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"重要",
"priseNum":"222"
},{
"username":"施",
"createTime":"04-23 13:12",
"commentNum":"12",
"content":"啦啦啦",
"headIcon":"http://www.haopic.me/wp-content/uploads/2015/12/2015122808171644.jpg",
"images":"http://img1.hq2011.com/uploads/allimg/150131/0122205Y4-0.jpg",
"level":"公告",
"priseNum":"4"
},{
"username":"宋",
"createTime":"04-23 15:45",
"commentNum":"23",
"content":"呵呵呵",
"headIcon":"http://scimg.jb51.net/allimg/170330/2-1F3301636200-L.jpg",
"images":"http://img1.3lian.com/2015/w7/85/d/21.jpg",
"level":"公告",
"priseNum":"12"
},{
"username":"杨",
"createTime":"04-23 13:16",
"commentNum":"12",
"content":"嘻嘻嘻",
"headIcon":"http://www.qqw21.com/article/UploadPic/2016-2/20162192155654059.jpg",
"images":"http://img1.hq2011.com/uploads/allimg/150131/0122205Y4-0.jpg",
"level":"公告",
"priseNum":"1"
},{
"username":"程",
"createTime":"04-22 14:12",
"commentNum":"22",
"content":"哈哈哈",
"headIcon":"http://img2.imgtn.bdimg.com/it/u=1576185143,2361770572&fm=214&gp=0.jpg",
"images":"http://pic.58pic.com/58pic/13/70/90/29358PICQjG_1024.jpg",
"level":"重要",
"priseNum":"23"
}
]
}
那么,怎么读到这些数据呢?使用系统的方法:
public static String getJson(String fileName,Context context) {
//将json数据变成字符串
StringBuilder stringBuilder = new StringBuilder();
try {
//获取assets资源管理器
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();
}
这个时候,如果我们要把字符串转换成Object的话:
JSONObject jsonObject = new JSONObject(str);
然后通过正常的转换,获取其中的JSONArray或者直接转成对象:
//将读出的字符串转换成JSONobject
JSONObject jsonObject = new JSONObject(str);
//获取JSONObject中的数组数据
jsonArray = jsonObject.getJSONArray(arrName);
如果我们使用Gson解析的话:
Type listType = new TypeToken<List<SearchCompanyResultBean>>() {
}.getType();
//这里的json是字符串类型 = jsonArray.toString();
List<SearchCompanyResultBean> list = new Gson().fromJson(json, listType );
这里我的实体类是SearchCompanyResultBean。
通过Gson将json数组转换成list集合
有了数据源,然后在设置到ui上
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)