如何在android中读取json文件?

时间:2015-07-21 15:34:23

标签: android json

我创建了一个json文件。此文件需要复制到另一个文件。这是json文件。

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

我正在读取此文件并将其放入另一个文件中。这就是我的尝试。

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

但这不起作用。这意味着它不会读取整个文件。它只读取第一个节点。我怎样才能读取整个文件?

2 个答案:

答案 0 :(得分:1)

我认为根本不需要解析JSON来复制文件。但是,您的JSON似乎无效。如果您需要解析JSON以便通过一些修改来复制数据,那么您必须使JSON有效。

答案 1 :(得分:1)

将您的JSON归为

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

然后,您可以像这样解析它:

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