0

i have a variable of type String and i need create a json file with this variable.

I need to create JSON file with content of variable "result".

I hope that you can help me!

Thanks in advance!

@TargetApi(Build.VERSION_CODES.KITKAT)
public String showResult(View v) throws IOException {

        String  result = "";

        Gson json = new Gson();
        for (Planet p : plAdapter.getBox()) {
            if (p.isSelected()){
                /*json.put("name",p.getName());
                json.put("distance",p.getDistance());
                json.put("quantity",p.getQuantità());*/
                result=result+json.toJson(p);


            }
        }
         //result=json.toString();

        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/droidText/";

        //FileWriter file=null;

    /*  try {
            file = new FileWriter(path+"filename5.json");
            file.write(result);


        }catch(IOException ie){}
        finally{


            file.flush();
            file.close();
        }*/try(FileWriter file = new FileWriter(path + "filename.json"))  {
        //  file=new FileWriter(path+"filename100.json");
        /*FileWriter file = new FileWriter(path+"filename31.json");
            file.write(result);
        file.close();
        */
            json.toJson(result);
            //System.out.println("Successfully wrote Json result to file.");
        /*catch(IOException r){

        }*/
        }catch (IOException e){

        }
        System.out.println(result);
        return result;

    }
1
  • create JSONObject using all values Commented Jun 15, 2016 at 7:35

2 Answers 2

2
public static String showResult(View v) {
        String  result = "";
        JsonObject json = new JsonObject():
         for (Planet p : plAdapter.getBox()) {
                    if (p.isSelected()){
                       json.put("name",p.getName());
                       json.put("distance",p.getDistance());
                       json.put("quantity",p.getQuantità());
                    }
           }
         result = json.toString();
         FileWriter file = null;
         try { 
                file = new FileWriter("/path/filename.json");
                file.write(result); 
         }catch(IOException ie){}
         finally{ 
           file.flush();
           file.close();
         }
     return result;
}
Sign up to request clarification or add additional context in comments.

10 Comments

he needs to create a json file not JSon object
This does not answer the question.
@rajanks with your example i store on smartphone file JSON? i need to store it
can i get a screeshot?
@rajanks can you help me please? I added screenshot under your code
|
1

A little modification to the same answer above by @rajan-ks. This one uses try-with-resource statement to declare a FileWriter instance. You don't need to close the FileWriter instance and also no need to catch the IOException as it is suppressed (as per [Java Language Specification 14.20.3])

Update 1: Updated showResult() to work with gson.

public static String showResult(View v) throws IOException {
    String result = "";
    Gson json = new Gson();

    for (Planet p : plAdapter.getBox()) {
        if (p.isSelected()) {
            result = result + json.toJson(p);
        }
    }

    String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/droidText/";

    try (FileWriter file = new FileWriter(path + "filename.json")) {
        json.toJson(result, file);
    }

    return result;
}

16 Comments

when i click on JSON file to open it with JSON viewer on my smartphone, the file is all white...
@ZAO: Which JSON framework you're using, as this works at my end? I'm using Google Gson, though not on Android.
with json viewer(app for Android) it doesn't work....you can try with my same app and you can see that it doesn't work
No, what I meant was which gradle dependency provides you with JsonObject (Jackson, or Gson, or JSON.simple, or something else)? Also, can you share the exact showResult method that you're using?
my grade dependency is compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:design:23.3.0' compile 'com.android.support:support-v4:23.3.0' compile files('libs/tinydb-0.0.9.jar') compile files('libs/droidText.0.2.jar')...now i edited code so you can see my method complete
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.