2013-08-02 17 views
0

一個JSON文件在我的應用程序,我將創建一個包含這類信息的JSON文件:創建的Android

{ 
    "sites":{ 
     "site":[ 
     { 
      "src":"/Users/redoddity/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/EE5BB071-A217-4C8D-99BA-5B6F392889A6/Documents/Sito3.html", 
      "name":"Sito3", 
      "expiryDate":"29 Ago 2013" 
     }, 
     { 
      "src":"/Users/redoddity/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/EE5BB071-A217-4C8D-99BA-5B6F392889A6/Documents/Sito2.html", 
      "name":"Sito2", 
      "expiryDate":"29 Ago 2013" 
     } 
     ] 
    } 
} 

(這是我的iPhone應用程序創建JSON)。現在我需要爲Android應用程序創建一個JSON文件。我發現這個:create json in android,但是這會創建一個沒有數組的JSON(你可以在我的JSON中看到我有一個網站的數組)????????????????????????

第二個問題,你可以看到,在我的JSON有我在iPhone的文檔文件夾中下載的HTML文件的路徑,現在的Android我保存這段代碼的HTML:

File file = getFileStreamPath(titleText + ".html"); 
    if (!file.exists()) { 
      try { 
     OutputStreamWriter out = new OutputStreamWriter(openFileOutput(titleText + ".html", 0)); 
     out.write(html); 
     out.close(); 
     } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
     } 
    } else { 
      Log.d("FILE", "Il file esiste"); 
      alert.show(); 
} 

如何我可以知道這個文件存儲的路徑嗎?

謝謝

回答

3

使用getFileStreamPath

File file = getFileStreamPath(titleText + ".html"); 

從DOC

返回在其中具有 openFileOutput(字符串,整數)創建的文件存儲在文件系統中的絕對路徑。

JSONObject root = new JSONObject(); 
JSONArray arr = new JSONArray(); 
root.put("key",(Object)arr); 
+0

好的,非常感謝您回答我提出的第二個問題,第一個問題,我如何在內部使用JSON做一個數組? – lucgian84

+0

這件事情是怎麼回事? Android有它自己的庫以更簡單的方式創建json – Blackbelt

+0

好吧我會看看,謝謝你的其他答案 – lucgian84

0

我用這個方法中的Android創建JSON,用數據的內陣列,其中所述陣列以外的一些元數據。我敢肯定,你可以破解它以適應。

public final static JSONObject writeListJSON(ArrayList aGroupedList, 
     String username) throws Exception { 

    ArrayList aList = (ArrayList) aGroupedList.get(0); 
    JSONObject obj = new JSONObject(); 
    try { 
     String sub_id = (String) aList.get(14); 

     obj.put("username", 
       URLEncoder.encode(BtoCryptex.btoEncrypt(username), "UTF-8")); 
     obj.put("proj_id", "BT"); 
     obj.put("inner_count", "" + aGroupedList.size()); 
     obj.put("device", android.os.Build.MODEL); 
     obj.put("version", android.os.Build.VERSION.RELEASE); 
     obj.put("protocol_id", 
       URLEncoder.encode((String) aList.get(13), "UTF-8")); 
     obj.put("date", URLEncoder.encode((String) aList.get(2), "UTF-8")); 
     obj.put("time", URLEncoder.encode((String) aList.get(10), "UTF-8")); 
     obj.put("endtime", 
       URLEncoder.encode((String) aList.get(15), "UTF-8")); 
     obj.put("complete", 
       URLEncoder.encode((String) aList.get(16), "UTF-8")); 
     obj.put("placename", 
       URLEncoder.encode((String) aList.get(4), "UTF-8")); 
     obj.put("gridref", 
       URLEncoder.encode((String) aList.get(5), "UTF-8")); 
     obj.put("sub_id", URLEncoder.encode(sub_id, "UTF-8")); 
     for (int i = 0; i < aGroupedList.size(); i++) { 
      JSONObject innerobj = new JSONObject(); 
      ArrayList bList = (ArrayList) aGroupedList.get(i); 
      innerobj.put("species_name", 
        URLEncoder.encode((String) bList.get(3), "UTF-8")); 
      innerobj.put("count", 
        URLEncoder.encode((String) bList.get(6), "UTF-8")); 
      innerobj.put("breed", 
        URLEncoder.encode((String) bList.get(7), "UTF-8")); 
      innerobj.put("sensitive", 
        URLEncoder.encode((String) bList.get(9), "UTF-8")); 
      innerobj.put("comment", 
        URLEncoder.encode((String) bList.get(11), "UTF-8")); 
      obj.put("INNER" + (i + 1), innerobj); 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    // System.out.println(obj.toString()); 
    return obj; 
}