如何用对象和数组写一个空的json文件?

时间:2017-11-22 10:39:42

标签: android json

我在android设备的内部存储中有一个空的json文件。我需要用json数据的流动格式编写它,

{
  "data": [

  ]
}

我怎么能这样做?

我尝试使用以下代码段来执行我想要的操作,

BufferedWriter bufferedWriter = null;
FileWriter fileWriter = null;

    JSONObject JsonObject = new JSONObject();
    JSONArray array = new JSONArray();
    try {
        JsonObject.put("data",array);
        fileWriter = new FileWriter(file);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(JsonObject.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

但是没有任何内容写入文件,Logcat就像,

11-22 16:27:43.833 26727-26727/? I/art: Late-enabling -Xcheck:jni
11-22 16:27:43.849 26727-26735/? E/art: Failed sending reply to debugger: Broken pipe
11-22 16:27:43.849 26727-26735/? I/art: Debugger is no longer active
11-22 16:27:43.849 26727-26735/? I/art: Starting a blocking GC Instrumentation
11-22 16:27:43.942 26727-26727/? W/System: ClassLoader referenced unknown path: /data/app/com.diary.pocket.pocketdiary-2/lib/arm64
11-22 16:27:43.953 26727-26727/? I/InstantRun: starting instant run server: is main process
11-22 16:27:43.956 26727-26727/? V/Font: Change font:1
11-22 16:27:44.003 26727-26727/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-22 16:27:44.105 26727-26745/? I/Adreno: QUALCOMM build                   : 853a1ff, I9c435c2712
                                       Build Date                       : 01/10/17
                                       OpenGL ES Shader Compiler Version: XE031.09.00.04
                                       Local Branch                     : 
                                       Remote Branch                    : 
                                       Remote Branch                    : 
                                       Reconstruct Branch               : 
11-22 16:27:44.109 26727-26745/? I/OpenGLRenderer: Initialized EGL, version 1.4
11-22 16:27:44.109 26727-26745/? D/OpenGLRenderer: Swap behavior 1
11-22 16:27:45.286 26727-26760/com.diary.pocket.pocketdiary D/AppTracker: App Event: start
11-22 16:27:45.866 26727-26735/? I/art: Ignoring second debugger -- accepting and dropping
11-22 16:27:46.107 26727-26777/? D/AppTracker: App Event: stop
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
11-22 16:27:46.158 26727-26727/? V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
11-22 16:27:46.161 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@cb830d0
11-22 16:27:46.161 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@a2c58c9
11-22 16:27:46.162 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@c653bce
11-22 16:27:46.162 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@ea76aef
11-22 16:27:46.251 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@e4dd732
11-22 16:27:46.251 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@1620c83
11-22 16:27:46.323 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9b3a5ad
11-22 16:27:46.331 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4deb92e
11-22 16:27:46.331 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@6d50ccf
11-22 16:27:46.340 26727-26744/? D/AppTracker: App Event: start
11-22 16:27:46.475 26727-26727/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
11-22 16:27:48.941 26727-26727/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@3b97443

帮助我。提前致谢

1 个答案:

答案 0 :(得分:0)

您可以简单地执行此操作,如果您想在空文件中添加所需的结构。

try {
    FileWriter writer = new FileWriter(file);
    writer.append("{\"data\":[]}");
    writer.flush();
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

我希望这会对你有所帮助