Sample code for reading and writing JSON files developed by Android

JSON format is a common form of reading and writing. Reading and writing JSON files is also a common operation.

Let's practice reading and writing JSON files this time.

First of all, the read-write permission on the SD card must be applied. Then, the old method is to add < uses permission Android: name = "Android. Permission. Write_external_storage" / > in the androidmanifest.xml file

This is sufficient for systems below Android 6.0. 6.0 and above, you need to apply for SD card read-write permission in the code.

First, build the interface, two buttons and a text display component.

Default interface

Add button action in oncreate method

To write the JSON file, first judge the read-write permission of the SD card. If you don't have permission, you should apply for permission. If you have permission, you can execute the write operation.

The savesinglejson () method is a concrete implementation of writing JSON.

Note that writer. Close() cannot be lost, or you won't be able to write it to the file.

The location where the JSON file in the code is saved is stored in a variable

Read the JSON file. One is to read the records in the list circularly, the other is to read the fields in the records circularly, and the two loops are nested.

If it is a higher version of Android, it will make a judgment on the permission for the first time. According to the selection given by the operator's permission, there will be a corresponding system response. This is a method override for permission selection results. If you agree to the permission, write it. If you disagree, a prompt message will be displayed.

Interface

Save Jason

Generate file

Show Jason

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>