简体   繁体   English

使用Array列表创建JSON文件时出现问题

[英]Problem creating a JSON file with an Array list

I'm creating a function which retrieves the installed apps of the device, the package of the app and the permissions each app has. 我正在创建一个功能,用于检索设备的已安装应用程序,应用程序包以及每个应用程序具有的权限。 The problem is that I want to put all of this into a JSON file and I do not know how to do it, here it is what I programmed, but it doesn't work, please, if someone know something, help, it is very important for me! 问题是我想把所有这些都放到一个JSON文件中,我不知道怎么做,这里是我编程的,但它不起作用,请,如果有人知道的话,帮助,它是对我来说非常重要! Thanks in advance. 提前致谢。

public static void installedApps(Context context) throws IOException{

    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packagelist = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    CharSequence app;
    String pckge;
    String[] permissions;

    for (ApplicationInfo applicationInfo : packagelist){

        app = applicationInfo.loadLabel(context.getPackageManager());
        pckge = applicationInfo.packageName;

        JSONObject json = new JSONObject();

        Log.d("test", "App: "+ applicationInfo.loadLabel(context.getPackageManager()) +", Package: " + applicationInfo.packageName);

        try{
            PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);

            //Get Permissions
            String[] requestedPermissions = packageInfo.requestedPermissions;

            if(requestedPermissions != null){
                for (int i = 0; i < requestedPermissions.length; i++){

                    Log.d("test", requestedPermissions[i]);

                    permissions = requestedPermissions;

                    JSONArray jsonArray = new JSONArray();

                    try{
                        json.put("app", app);
                        json.put("package", pckge);

                        jsonArray.put(Integer.parseInt("permission"), permissions);


                    }catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }
            //primero cargar en un array todos los datos, y pasarlos a un json llamandolos
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }

    }
}

I would probably go for something like this: 我可能会这样做:

public static void installedApps(Context context) {
    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packagelist = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    JSONObject resp = new JSONObject();

    for (ApplicationInfo applicationInfo : packagelist) {

        CharSequence app = applicationInfo.loadLabel(context.getPackageManager());
        String pckge = applicationInfo.packageName;
        JSONObject json = new JSONObject();

        try {
            //Get Permissions
            PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
            String[] requestedPermissions = packageInfo.requestedPermissions;
            if (requestedPermissions != null) {
                json.put("app", app);
                json.put("package", pckge);
                JSONArray jsonArray = new JSONArray();
                for (String requestedPermission : requestedPermissions) {
                    jsonArray.put(requestedPermission);
                }
                json.put("permission", jsonArray);
                resp.put(applicationInfo.name, json); // something to identify each app
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // return resp.toString() or store in file or whatever you want to do
}

I am petty sure there is a problem with this line, 我很确定这条线有问题,

permissions = requestedPermissions; 

... ...

jsonArray.put(Integer.parseInt("permission"), permissions); 

since you define the permissions array at the top, you will see the final requestPermission entry adding to jsonArray over and over packageList.size() times. 由于您在顶部定义了permissions数组,因此您将看到最终的requestPermission条目jsonArray遍地添加到jsonArray packageList.size()次。

bring permission to inside the loop. permission带入循环内部。 it will solve your problem. 它会解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM