LocalJsonAnalyzeUtil.java
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.huaheng.wms.util;
import android.content.Context;
import android.content.res.AssetManager;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by xhu_ww on 2018/6/1.
* description:获取本地Json文件并 解析
*/
public class LocalJsonAnalyzeUtil {
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();
}
public static <T> T JsonToObject(String json, Class<T> type) {
Gson gson = new Gson();
return gson.fromJson(json, type);
}
public static <T> T JsonToObject(Context context, String fileName, Class<T> type) {
Gson gson = new Gson();
return gson.fromJson(getJson(context, fileName), type);
}
}