I have created a json file. This file need to copy to a another file.This is the json file.
[
{
"Number": "0123456789",
"Date": "Tue Jul 21 14:30:34 GMT+00:00 2015",
"message": "Hello i'm older"
}
][
{
"Number": "0123456789",
"Date": "Tue Jul 21 14:35:22 GMT+00:00 2015",
"message": "Hello i'm older"
}
][
{
"Number": "0123456789",
"Date": "Tue Jul 21 14:35:24 GMT+00:00 2015",
"message": "Hello i'm older"
}
]
I'm reading this file and put it in another file. This is what i tried.
private void copyFile(File file) throws IOException {
String TAG = "MyTest";
Toast.makeText(this, "copyFile", Toast.LENGTH_SHORT).show();
FileInputStream in = openFileInput("backupMessage.json");
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
reader.beginArray();
while (reader.hasNext()) {
String number = null;
String date = null;
String message = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("Number")) {
number = reader.nextString();
Log.d(TAG, number);
} else if (name.equals("Date")) {
date = reader.nextString();
Log.d(TAG, number);
} else if (name.equals("message")) {
message = reader.nextString();
Log.d(TAG, number);
} else {
reader.skipValue();
Log.d(TAG, "skip");
}
}
if(number != null && date != null && message != null){
writer(file, number, date, message);
Log.d(TAG, "called writer");
}
reader.endObject();
}
reader.endArray();
reader.close();
}
private void writer(File file,String number,String date, String message){
Toast.makeText(this, "writer", Toast.LENGTH_SHORT).show();
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file, true);
JsonWriter jwriter = new JsonWriter(new OutputStreamWriter(stream,"UTF-8"));
jwriter.setIndent(" ");
jwriter.beginArray();
jwriter.beginObject();
jwriter.name("Number").value(number);
jwriter.name("Date").value(date);
jwriter.name("message").value(message);
jwriter.endObject();
jwriter.endArray();
jwriter.close();
stream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
But this is not working. Thats means it is not reading the whole file. it is reading only a first node.How can i read the whole file ?
I don't think it is needed to parse the JSON in order to copy the file at all. BUT, your JSON seems to be invalid . If you need to parse the JSON in order to copy your data with some modifications, you must make your JSON valid.
CHange your JSON to
[
{
"Number": "0123456789",
"Date": "Tue Jul 21 14:30:34 GMT+00:00 2015",
"message": "Hello i'm older"
},
{
"Number": "0123456789",
"Date": "Tue Jul 21 14:35:22 GMT+00:00 2015",
"message": "Hello i'm older"
},
{
"Number": "0123456789",
"Date": "Tue Jul 21 14:35:24 GMT+00:00 2015",
"message": "Hello i'm older"
}
]
Then, you can parse it like that:
try
{
JSONObject tmp = null;
JSONArray array = new JSONArray(YOUR_JSON_AS_A_STRING);
for (int i=0; i<array.length(); i++)
{
tmp = array.getJSONObject(i);
writer(file, tmp.getString("Number"), tmp.getString("Date"), tmp.getString("message"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.