lucgian84
lucgian84

Reputation: 843

Create a JSON file in android

In my app I will create a JSON file that contains this kind of information:

{
   "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"
         }
      ]
   }
}

(this is the JSON I create for the iPhone app). Now I need to create a JSON file for the Android app.

First question: how I can create a JSON in Android, on here I found this: create json in android, but this will create a JSON without array (you can see in my JSON I've an array of site)?

Second question: you can see that in my JSON there are the path of the html file I downloaded in the Documents folder of iPhone, now in Android I stored the html with this code:

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

How I can know the path where this file are stored?

Thank you

Upvotes: 1

Views: 19495

Answers (2)

Blackbelt
Blackbelt

Reputation: 157467

use getFileStreamPath

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

From the doc

Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

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

Upvotes: 3

iaindownie
iaindownie

Reputation: 1056

I use this method to create JSON in Android, with an inner array of data, with some metadata outside the array. I'm sure you can hack it to fit.

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

Upvotes: 0

Related Questions