I am creating an Android application that creates a text file with JSON objects and writes it to internal storage. For this, I have the following code:
JSONObject myJSON = new JSONObject();
try {
myJSON.put("Length", trim)
.put("Website", data)
.put("Id", tx);
} catch (JSONException e1) {
e1.printStackTrace();
}
String myJSONString = myJSON.toString();
myJSONString += ", ";
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
fos.write(myJSONString.getBytes());
fos.close();
} catch (Exception e) {
Log.d(TAG, "cought");
e.printStackTrace();
}
Now I get a text file that looks like this:
{"Id":"20101211T155146","Length":10}, {"Id":"20101211T155155","Length":10},
{"Id":"20101211T155203","Length":10}, {"Id":"20101211T155252","Length":10},
I would like to collect this data in a JSON file. An application should write, store and retrieve JSON. The problem is that when I iterate over JSON objects from a 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";
}
He gives an error. I got the parsing code from a large tutorial at: http://www.ibm.com/developerworks/web/library/x-andbene1/?ca=drs-#author1
In this example, the code expects brackets around the entire file and without a comma after the last object. Therefore, I will need:
[{"Id":"20101211T155146","Length":10}, {"Id":"20101211T155155","Length":10},
{"Id":"20101211T155203","Length":10}, {"Id":"20101211T155252","Length":10}]
JSON ; JSON, ?
UPDATE:
, JSON , , JSON . :
[
{
"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"
}
]
, , , ?