Android解析Asset目录下的json文件

Android解析Asset目录下的json文件,第1张

概述在appmodule中的src/main/assets目录下我们准备了两个json文件:destination.json如下:{"mainabs/sofa":{"isFragment":true,"asStarter":false,"needLogin":false,"pageUrl":"mainabs/sofa",

在app module中的src/main/assets目录下我们准备了两个Json文件:

destination.Json如下:

{  "main/tabs/sofa": {    "isFragment": true,    "asstarter": false,    "needLogin": false,    "pageUrl": "main/tabs/sofa",    "classname": "com.test.ppjoke.ui.notifications.NotificationsFragment",    "ID": 448706824  },  "main/tabs/dash": {    "isFragment": true,    "asstarter": false,    "needLogin": false,    "pageUrl": "main/tabs/dash",    "classname": "com.test.ppjoke.ui.dashboard.DashboardFragment",    "ID": 938694224  },  "main/tabs/home": {    "isFragment": true,    "asstarter": true,    "needLogin": false,    "pageUrl": "main/tabs/home",    "classname": "com.test.ppjoke.ui.home.HomeFragment",    "ID": 509754652  },  "main/tabs/my": {    "isFragment": true,    "asstarter": false,    "needLogin": false,    "pageUrl": "main/tabs/my",    "classname": "com.test.ppjoke.ui.my.MyFragment",    "ID": 750793084  }}

对应的destination的javabean文件:

public class Destination {    public String pageUrl;    public int ID;    public boolean needLogin;    public boolean asstarter;    public boolean isFragment;    public String classname;}

 main_tabs_config.Json如下:

{  "activecolor": "#333333",  "inActivecolor": "#666666",  "selectTab": 0,  "tabs": [    {      "size": 24,      "enable": true,      "index": 0,      "pageUrl": "main/tabs/home",      "Title": "首页"    },    {      "size": 24,      "enable": true,      "index": 1,      "pageUrl": "main/tabs/sofa",      "Title": "沙发"    },    {      "size": 40,      "enable": true,      "index": 2,      "tintcolor": "#ff678f",      "pageUrl": "main/tabs/publish",      "Title": ""    },    {      "size": 24,      "enable": true,      "index": 3,      "pageUrl": "main/tabs/dash",      "Title": "发现"    },    {      "size": 24,      "enable": true,      "index": 4,      "pageUrl": "main/tabs/my",      "Title": "我的"    }  ]}

对应的Bottombar的JavaBean文件如下:

import java.util.List;public class Bottombar {    /**     * activecolor : #333333     * inActivecolor : #666666     * tabs : [{"size":24,"enable":true,"index":0,"pageUrl":"main/tabs/home","Title":"首页"},{"size":24,"enable":true,"index":1,"pageUrl":"main/tabs/sofa","Title":"沙发"},{"size":40,"enable":true,"index":2,"tintcolor":"#ff678f","pageUrl":"main/tabs/publish","Title":""},{"size":24,"enable":true,"index":3,"pageUrl":"main/tabs/find","Title":"发现"},{"size":24,"enable":true,"index":4,"pageUrl":"main/tabs/my","Title":"我的"}]     */    public String activecolor;    public String inActivecolor;    public List<Tab> tabs;    public int selectTab;//底部导航栏默认选中项    public static class Tab {        /**         * size : 24         * enable : true         * index : 0         * pageUrl : main/tabs/home         * Title : 首页         * tintcolor : #ff678f         */        public int size;        public boolean enable;        public int index;        public String pageUrl;        public String Title;        public String tintcolor;    }}

读取Json并解析的java代码:

import androID.content.res.AssetManager;import com.alibaba.fastJson.JsON;import com.alibaba.fastJson.TypeReference;import com.test.ppjoke.model.Bottombar;import com.test.ppjoke.model.Destination;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;public class AppConfig {    private static HashMap<String, Destination> sDestConfig;    private static Bottombar sBottombar;    public static HashMap<String, Destination> getDestConfig() {        if (sDestConfig == null) {            String content = parsefile("destination.Json");            sDestConfig = JsON.parSEObject(content, new TypeReference<HashMap<String, Destination>>() {            });        }        return sDestConfig;    }    public static Bottombar getBottombarConfig() {        if (sBottombar == null) {            String content = parsefile("main_tabs_config.Json");            sBottombar = JsON.parSEObject(content, Bottombar.class);        }        return sBottombar;    }    private static String parsefile(String filename) {        AssetManager assets = AppGlobals.getApplication().getAssets();        inputStream is = null;        BufferedReader br = null;        StringBuilder builder = new StringBuilder();        try {            is = assets.open(filename);            br = new BufferedReader(new inputStreamReader(is));            String line = null;            while ((line = br.readline()) != null) {                builder.append(line);            }        } catch (IOException e) {            e.printstacktrace();        } finally {            try {                if (is != null) {                    is.close();                }                if (br != null) {                    br.close();                }            } catch (Exception e) {            }        }        return builder.toString();    }}

 

总结

以上是内存溢出为你收集整理的Android解析Asset目录下的json文件全部内容,希望文章能够帮你解决Android解析Asset目录下的json文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1042976.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存