2016-06-15 3 views
1

で開いたときにJSONファイルが空になります。ファイルが空です...コンテンツが表示されません...なぜですか?スマートフォン<code>JSON VIEWER</code>のアプリで<code>JSON file</code>を開くと、アプリケーションjsonビューア

は今、私はあなたに私は、ファイルjsonを保存する方法を私のコードを表示....事前みんなで

ありがとう!

public String showResult(View v) throws IOException { 
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(); 

     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(); 
     }*/ 
     FileWriter file = new FileWriter(path+"filename31.json"); 
      file.write(result); 
      file.close(); 
      System.out.println("Successfully wrote Json result to file."); 
     /*catch(IOException r){ 

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

    } 

JsonObjectクラス:

private static class JsonObject { 
     public void put(String name, String name1) { 
     } 
    } 
+0

Jsonファイルに保存しようとしていますか? – PVR

+0

チェックボックスの名前が –

答えて

0

あなたはFileWriterを閉じられることはありません。常にReaderWriterオブジェクトを閉じます。代わりに、完璧なJSON文字列を生成するカスタムJsonObject使用com.google.gson.JsonObject

FileWriter file = new FileWriter(path+"filename31.json"); 
file.write(result); 
file.close(); 
+0

に変更されました。私のアプリケーションのパッケージ名が表示されています... jsonファイルが表示されません... –

0

使用gson.jar

JsonObject json = new JsonObject(); 
     json.addProperty("name", "s"); 
     json.addProperty("distance", "2"); 
     json.addProperty("quantity", "2"); 

     result = json.toString(); 

文字列は次のようになります。

{ "名": "S"、 "距離": "2"、 "量": "2"}あなたは何

+0

gson.jarがインポートされました。 –

+0

import com.google.gson.JsonObject; JsonObjectを所有する代わりに – PVR

関連する問題