我不知道为什么它不起作用,我想我正确地做到了。
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: FATAL EXCEPTION: main
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: Process: com.imaginarv.launcher, PID: 11957
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: java.lang.StackOverflowError
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
此文件存储在assets / apps.json中。 我在http://jsonviewer.stack.hu/.
上查看了它[
{
"name": "Carpeta 1",
"packages": [
"com.mobilendo.app1",
"com.mobilendo.app2"
]
},
{
"name": "Carpeta 2",
"packages": [
"com.mobilendo.app1",
"com.mobilendo.app2"
]
}
]
从apps.json读取JSon并解析它的函数。
private ArrayList<FolderModel> getFolders() {
String jsonString = loadJSONFromAsset();
Gson gson = new Gson();
ArrayList<FolderModel> foldersList;
foldersList = gson.fromJson(jsonString, new TypeToken<ArrayList<FolderModel>>(){}.getType());
return foldersList;
}
public String loadJSONFromAsset() {
StringBuilder buf = new StringBuilder();
try {
InputStream json = getAssets().open("apps.json");
BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buf.toString();
}
public class FolderModel {
@SerializedName("name")
private String name;
@SerializedName("packages")
private ArrayList<String> packages;
@SerializedName("apps")
private ArrayList<AppModel> apps;
@SerializedName("icon")
private Drawable icon;
public FolderModel(String name, ArrayList<String> packages) {
this.name = name;
this.packages = packages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getPackages() {
return packages;
}
public void setPackages(ArrayList<String> packages) {
this.packages = packages;
}
public ArrayList<AppModel> getApps() {
return apps;
}
public void setApps(ArrayList<AppModel> apps) {
this.apps = apps;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}
public class AppModel implements Serializable {
private final Context mContext;
private final ApplicationInfo mInfo;
private String mAppLabel;
private Drawable mIcon;
private boolean mMounted;
private final File mApkFile;
public AppModel(Context context, ApplicationInfo info) {
mContext = context;
mInfo = info;
mApkFile = new File(info.sourceDir);
}
public ApplicationInfo getAppInfo() {
return mInfo;
}
public String getApplicationPackageName() {
return getAppInfo().packageName;
}
public String getLabel() {
return mAppLabel;
}
public Drawable getIcon() {
if (mIcon == null) {
if (mApkFile.exists()) {
mIcon = mInfo.loadIcon(mContext.getPackageManager());
return mIcon;
} else {
mMounted = false;
}
} else if (!mMounted) {
// If the app wasn't mounted but is now mounted, reload
// its icon.
if (mApkFile.exists()) {
mMounted = true;
mIcon = mInfo.loadIcon(mContext.getPackageManager());
return mIcon;
}
} else {
return mIcon;
}
return mContext.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
}
public void loadLabel(Context context) {
if (mAppLabel == null || !mMounted) {
if (!mApkFile.exists()) {
mMounted = false;
mAppLabel = mInfo.packageName;
} else {
mMounted = true;
CharSequence label = mInfo.loadLabel(context.getPackageManager());
mAppLabel = label != null ? label.toString() : mInfo.packageName;
}
}
}
}
答案 0 :(得分:0)
尝试这样的事情:
JSONArray list;
list = new JSONArray(jsonString);
foldersList = new ArrayList<FolderModel>();
FolderModel f;
for (int i = 1; i < list.length(); ++i) {
f=new FolderModel();
JSONObject obj = list.getJSONObject(i);
f.setName(obj.getString("name").toString());
}
答案 1 :(得分:0)
我通常使用另一种解析方式:
FolderModel[] arrayModels = gson.fromJson(jsonString, FolderModel[].class);
答案 2 :(得分:0)
从FolderModel和AppModel中删除Drawable对象,并将其另存为id或image url。 Drawables不是可序列化的条款,将它们保存在内存中是很昂贵的。