2014-01-30 64 views
1

我正在使用JSON。我寫了可以解析JSON並顯示列表視圖(圖像和文本)的代碼。 現在我想將我的JSON保存在文件(json.txt)中。 這是我的代碼。我試圖挽救JSON,但是當我調試它保存的只是第一個數據我json.txt文件,但我在JSON 20點的數據,如果有人知道的解決方案,請幫助.......Android從url寫入json文件

jsonparser = new JSONParser(); 

     JSONObject jsonobject = jsonparser.getJSONfromURL(URL); 
     try { 

      jsonarray = jsonobject.getJSONArray("data"); 

      for (int i = 0; i < jsonarray.length(); i++) { 
       jsonobject = jsonarray.getJSONObject(i); 

       HashMap<String, String> map = new HashMap<String, String>(); 

       map.put("journal", jsonobject.getString(KEY_journal)); 
       map.put("image", jsonobject.getString(KEY_image)); 
       map.put("title", jsonobject.getString(KEY_title)); 
       map.put("description", 
         jsonobject.getString(KEY_description)); 
       map.put("JournalID", jsonobject.getString(KEY_JournalID)); 
       map.put("pubDate", jsonobject.getString(KEY_pubDate)); 
       map.put("statID", jsonobject.getString(KEY_statID)); 

       Content cont = new Content(jsonobject.getString("journal"), 
         jsonobject.getString("image"), 
         jsonobject.getString("title"), 
         jsonobject.getString("pubDate"), 
         jsonobject.getString("description"), 
         jsonobject.getString("JournalID"), 
         jsonobject.getString("statID")); 
       contents.add(cont); 




        yourFile = new File("/sdcard/json.txt"); 

        try { 
         writer = new OutputStreamWriter(
           new FileOutputStream(yourFile), "UTF-8"); 
         writer.write(jsonobject.toString()); 
         writer.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } finally { 
         if (writer != null) { 
          try { 
           writer.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 

回答

0

開放你的文件處於追加模式。

new OutputStreamWriter(new FileOutputStream(yourFile,true), "UTF-8"); 
+0

我不明白你..我改變了但它但我有syntacs錯誤 – user3233500

+0

看到我更新的答案。如果它解決你的問題,然後標記爲解決。 – User10001

+0

問題沒有解決?我有問題了:( – user3233500

0

使用網址檢索到的JSONObject的獨立變量,用於循環中的數據陣列的一個:

jsonparser = new JSONParser(); 

     JSONObject jsonfromurl = jsonparser.getJSONfromURL(URL); 
     try { 

      jsonarray = jsonfromurl.getJSONArray("data"); 

      for (int i = 0; i < jsonarray.length(); i++) { 
       JSONObject jsonobject = jsonarray.getJSONObject(i); 
       HashMap<String, String> map = new HashMap<String, String>(); 

       map.put("journal", jsonobject.getString(KEY_journal)); 
       map.put("image", jsonobject.getString(KEY_image)); 
       map.put("title", jsonobject.getString(KEY_title)); 
       map.put("description", 
         jsonobject.getString(KEY_description)); 
       map.put("JournalID", jsonobject.getString(KEY_JournalID)); 
       map.put("pubDate", jsonobject.getString(KEY_pubDate)); 
       map.put("statID", jsonobject.getString(KEY_statID)); 

       Content cont = new Content(jsonobject.getString("journal"), 
         jsonobject.getString("image"), 
         jsonobject.getString("title"), 
         jsonobject.getString("pubDate"), 
         jsonobject.getString("description"), 
         jsonobject.getString("JournalID"), 
         jsonobject.getString("statID")); 
       contents.add(cont); 




        yourFile = new File("/sdcard/json.txt"); 

        try { 
         writer = new OutputStreamWriter(
           new FileOutputStream(yourFile), "UTF-8"); 
         writer.write(jsonfromurl.toString()); 
         writer.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } finally { 
         if (writer != null) { 
          try { 
           writer.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
0

您應該遵循相同的風格here。從我所看到的,他們實際上將字節寫入FileOutputStream,而您嘗試寫入一個字符串。根據documentation,FileOutputStream只接受字節。

改爲嘗試writer.write(jsonobject.toString().getBytes());

+0

謝謝你我改變了,但是當我把你的代碼我有syntacs錯誤 – user3233500

+0

它說什麼?我沒有看到任何錯誤。 – roarster