Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Internal Storage, how to properly parse a JSON text file

Tags:

json

android

I'm creating an Android app which creates a text file with JSON objects and writes it to internal storage. I have the following code to do that:

JSONObject myJSON = new JSONObject();
//Set the JSON object with website, length and Id (time-stamp)
try {
    myJSON.put("Length", trim)
    .put("Website", data)
    .put("Id", tx);
} catch (JSONException e1) {
    e1.printStackTrace();
}

//Convert JSON object to a string and add a comma
String myJSONString = myJSON.toString();
myJSONString += ", ";

try {
     FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
     fos.write(myJSONString.getBytes());
     fos.close();
     //Log.d(TAG, "Written to file");

} catch (Exception e) {
    Log.d(TAG, "cought");
    e.printStackTrace();
}

Now I get a text file which looks like so:

{"Id":"20101211T155146","Length":10}, {"Id":"20101211T155155","Length":10},
{"Id":"20101211T155203","Length":10}, {"Id":"20101211T155252","Length":10}, 

I'd like to now collect that data in the JSON file. The app needs to write, store and retrieve the JSON. The problem is when I go to parse the JSON objects from the file using:

String x = "";
InputStream is = this.getResources().openRawResource(R.raw.pwh);
byte [] buffer = new byte[is.available()];
while (is.read(buffer) != -1);
String jsontext = new String(buffer);
JSONArray entries = new JSONArray(jsontext);

x = "JSON parsed.\nThere are [" + entries.length() + "]\n\n";

int i;
for (i=0;i<entries.length();i++)
{
    JSONObject post = entries.getJSONObject(i);
    x += "------------\n";
    x += "Id:" + post.getString("Id") + "\n";
    x += "Length:" + post.getString("Length") + "\n\n";
}

It throws an error. I got the parsing code from a great tutorial at: http://www.ibm.com/developerworks/web/library/x-andbene1/?ca=drs-#author1 In that example, the code is expecting brackets around the whole file and no comma after the last object. So I would need:

[{"Id":"20101211T155146","Length":10}, {"Id":"20101211T155155","Length":10},
{"Id":"20101211T155203","Length":10}, {"Id":"20101211T155252","Length":10}]

But I write those JSON lines one at time in my code; How can I manipulate the JSON text file to get it in the expected format?

UPDATE:

The problem is still that if the user writes the JSON array to the file and then comes back and changes it again, you get two JSON arrays in that file. Like so:

[
     {
          "phonenumber": "15555215554",
          "time": "20110113T173835",
          "username": "edit username",
          "email": " edit email",
          "password": "edit password"
     }
][
     {
          "phonenumber": "15555215554",
          "time": "20110113T173900",
          "username": "edit username",
          "email": " edit email",
          "password": "edit password"
     },
     {
          "phonenumber": "15555215554",
          "time": "20110113T173900",
          "username": "edit username",
          "email": " edit email",
          "password": "edit password"
     }
]

How can I read the first array, add the second and then re-write the file with both arrays merged into one?

like image 454
Chris Avatar asked Dec 12 '10 01:12

Chris


Video Answer


1 Answers

You need to use JSONArray to create an array of JSON objects. Once you do a toString(), you will have the expected brackets. You do not need to manually add the commas either.

Take a look at http://developer.android.com/reference/org/json/JSONArray.html for more info

like image 155
Benoit Martin Avatar answered Sep 23 '22 16:09

Benoit Martin