【问题标题】:Create a JSON file in android在android中创建一个JSON文件
【发布时间】:2013-08-02 10:26:32
【问题描述】:

在我的应用程序中,我将创建一个包含此类信息的 JSON 文件:

{
   "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 文件。

第一个问题:如何在 Android 中创建 JSON,在这里我发现了这个:create json in android,但这将创建一个没有数组的 JSON(你可以在我的 JSON 中看到我有一个站点数组)?

第二个问题:你可以看到在我的JSON中有我在iPhone的Documents文件夹中下载的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();
}

我如何知道这个文件的存储路径?

谢谢

【问题讨论】:

    标签: android json path


    【解决方案1】:

    使用 getFileStreamPath

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

    来自文档

    返回文件系统上创建文件的绝对路径 openFileOutput(String, int) 被存储。

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

    【讨论】:

    • 好的,谢谢你回答我提出的第二个问题,第一个问题呢,我怎样才能做一个包含数组的 JSON?
    • 这是怎么回事? Android有自己的库以更简单的方式创建json
    • 我添加了一个例子。如您所见,编写复杂的 json 对象非常简单
    • 谢谢您的解释
    【解决方案2】:

    我使用这种方法在 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      相关资源
      最近更新 更多