1

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

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, thank you for answering the second question I made, what about the first, how I can do a JSON with an array inside it?
what`s the matter about it? Android has its own library to create json in a simpler way
I added an example. As you can see it is really simple to compose complex json objects
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.