Write JSON to a File

后端 未结 4 2014
南旧
南旧 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条回答
  •  旧时难觅i
    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();
            }
        });
    

提交回复
热议问题