Write JSON to a File

后端 未结 4 2011
南旧
南旧 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 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();
            }
        });
    

提交回复
热议问题