日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

安卓加载asset中的json文件_Android解析Asset目录下的json文件

發布時間:2023/12/9 Android 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓加载asset中的json文件_Android解析Asset目录下的json文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在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 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 sDestConfig;

private static BottomBar sBottomBar;

public static HashMap getDestConfig() {

if (sDestConfig == null) {

String content = parseFile("destination.json");

sDestConfig = JSON.parseObject(content, new TypeReference>() {

});

}

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();

}

}

總結

以上是生活随笔為你收集整理的安卓加载asset中的json文件_Android解析Asset目录下的json文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。