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;
}