Write JSON to a File

后端 未结 4 2010
南旧
南旧 2020-12-31 16:34

I have a class compositionJSON. The class has a method calls makeJSONObject, that creates a JSON-Object and put stuff in it. Here is the code of the class.

p         


        
相关标签:
4条回答
  • 2020-12-31 16:49

    makeJSONObject is returning JSONObject

    Your code should be

    saveCompo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setName();
                createJSONFolder();
                CompositionJso obj = new CompositionJso();
                JSONObject  jsonObject = obj.makeJSONObject(compoTitle, compoDesc, imgPaths, imageViewPaths);
                MyCompositionsListActivity.buildList();
    
                try {
                    Writer output = null;
                    File file = new File("storage/sdcard/MyIdea/MyCompositions/" + compoTitle + ".json");
                    output = new BufferedWriter(new FileWriter(file));
                    output.write(jsonObject.toString());
                    output.close();
                    Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();
    
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
                finish();
            }
        });
    
    0 讨论(0)
  • 2020-12-31 16:58

    Try this write a simple class with static methods to save and retrieve json object in a file :

    CODE :

    public class RetriveandSaveJSONdatafromfile {
    
     public static String objectToFile(Object object) throws IOException {
        String path = Environment.getExternalStorageDirectory() + File.separator + "/AppName/App_cache" + File.separator;
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        path += "data";
        File data = new File(path);
        if (!data.createNewFile()) {
            data.delete();
            data.createNewFile();
        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(data));
        objectOutputStream.writeObject(object);
        objectOutputStream.close();
        return path;
    }
    
    public static Object objectFromFile(String path) throws IOException, ClassNotFoundException {
        Object object = null;
        File data = new File(path);
        if(data.exists()) {
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(data));
            object = objectInputStream.readObject();
            objectInputStream.close();
        }
        return object;
    }
    } 
    

    To save json in a file use RetriveandSaveJSONdatafromfile.objectToFile(jsonObj) and to fetch data from file use

     path = Environment.getExternalStorageDirectory() + File.separator +   
     "/AppName/App_cache/data" + File.separator; 
     RetriveandSaveJSONdatafromfile.objectFromFile(path);
    
    0 讨论(0)
  • 2020-12-31 17:04

    Thanks @Chris Handy! I created a JSONObjectVariable and assign it to the makeJSONObject. Here is my final code:

    saveCompo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setName();
                createJSONFolder();
                CompositionJso compositionJso = new CompositionJso();
                JSONObject obj;
                obj = compositionJso.makeJSONObject(compoTitle, compoDesc, imgPaths, imageViewPaths);
                MyCompositionsListActivity.buildList();
    
                try {
                    Writer output;
                    File file = new File("storage/sdcard/MyIdea/MyCompositions/" + compoTitle + ".json");
                    output = new BufferedWriter(new FileWriter(file));
                    output.write(obj.toString());
                    output.close();
                    Toast.makeText(getApplicationContext(), "Composition saved", Toast.LENGTH_LONG).show();
    
    
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
                finish();
            }
        });
    
    0 讨论(0)
  • 2020-12-31 17:08

    In the method makeJSONObject you instantiate a new JSONObject.

    this code should work.

    public void makeJSONObject (String title, String desc, ArrayList<String> imgPath, ArrayList<Resources> imgView) {
    
        try {
            this.put("title", title);
            this.put("desc", desc);
            this.put("imgPath", imgPath);
            this.put("imgViewPath", imgView);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
提交回复
热议问题