Reading and modification of JSON data on the Unity Android side

1. Several ways to read the path

Here is a brief summary of the common ways to read paths when we develop Android:

  • Resources.Load<T>()
    • The Resources folder is a special folder. After the APK is packaged or the EXE file is generated, this external path will not exist. It is an internal dynamic loading resource. It is read-only and cannot be dynamically modified. Packing will be compressed and encrypted.
  • Application.streamingAssetsPath
    • The content of the StreamingAsset folder will be put into the package intact, and it is read-only on the Android side, but it can be read and written on the PC side, and must be placed in the root directory , otherwise it cannot be read. On the Android platform, the files in this directory are compressed into a single .jar file, and the data in the file can be read through WWW. The Android side cannot read the data by IO , and can be read through WWW or UnityWebRequest .
    • "jar:file://" + Application.dataPath + "!/assets/"(The last / should be careful not to omit)
  • Application.persistentDataPath
    • When the application is released to the IOS or Android side, this path will point to a public path (created at runtime). When the software is updated or overwritten, the data here will not be cleared. This path is readable and writable . Note that on IOS it is the sandbox of the application, but on Android it can be the sandbox of the program or sdcard, and when Android is packaged, there is an option Write Access on the ProjectSetting page, which can set its path to be the sandbox It is still sdcard; using the feature of being able to read and write, under the Android platform, the Json data in some StreamingAsset files will often be copied to this folder when the program is running, and the data will not be read or modified. Not all cases have to be copied under this file, and it depends on the case. (Tips: When you need to copy to this folder, you first need to determine whether the file exists)

Idea: Because the StreamingAsste folder is read-only on the Android side, but the Application.persistentDataPath folder is readable and writable, we can create a copy of the JSON data and save it in the Application.persistentDataPath directory for our reading and modification.


2. Code example Android reads and modifies JSON data ( introduction to basic JSON reading )

JSON data

{
    
    
    "StudentData":[
        {
    
    
            "ID":"001",
            "Name":"小明",
            "Age":"12"
        },
        {
    
    
            "ID":"002",
            "Name":"小花",
            "Age":"13"
        }
    ]
}

Copy from the StreamingAsset folder to the persistentDataPath folder

 //从StreamingAsset文件夹复制到persistentDataPath文件夹中
    IEnumerator CopyStreamingAssetData(string fileName)
    {
    
    
        string fromPath = "";

        if (Application.platform == RuntimePlatform.Android)
            fromPath = "jar:file://" + Application.dataPath + "!/assets/" + fileName;
        else
            fromPath = Application.streamingAssetsPath + "/" + fileName;

        string toPath = "jar:file://" + Application.persistentDataPath + "/" + fileName;

        UnityWebRequest request = UnityWebRequest.Get(fromPath);
        yield return request.SendWebRequest();

        if (!File.Exists(Application.persistentDataPath + "/" + fileName))
        {
    
    
            FileStream fs = File.Create(Application.persistentDataPath + "/" + fileName);
            fs.Close();
        }

        File.WriteAllBytes(Application.persistentDataPath + "/" + fileName, request.downloadHandler.data);
    }

Read data from persistentData folder

    
    IEnumerator ReadJson_IE(string path)
    {
    
    
        UnityWebRequest request = UnityWebRequest.Get("jar:file://" + Application.persistentDataPath + "/" + path);
        yield return request.SendWebRequest();
        data = JsonUtility.FromJson<Data>(request.downloadHandler.text);
        txt.text = request.downloadHandler.text+"\n当前的数量为:"+data.StudentData.Count;
    }

Corresponding JSON parsing class

[Serializable]
public class Data {
    
    

    public List<Student> StudentData;
}
[Serializable]
public class Student {
    
    

    public string ID;
    public string Name;
    public string Age;
}

Guess you like

Origin blog.csdn.net/weixin_44446603/article/details/121367200
Recommended