中文字幕无码a片久久,亚洲日韩在线观看浪潮,人人超人人超碰超国产二区,国产人妻久久一区二区,国产人妻久久一区二区

福感科技有限公司 歡迎您!
聯(lián)系方式

    地址:北京市平谷區(qū)馬坊鎮(zhèn)金河北街17號院3號樓7層712

    電話:010-89968230

    網(wǎng)站:http://0743119.com

Json文件操作(創(chuàng)建、讀取、解析、修改)

2025-4-17 23:28:07??????點擊:

創(chuàng)建Json文件


如果我們要先生成Json文件的,就可以使用IO命名空間的StreamWriter類:

首先,我們先寫一個字段類Person,類里面有string類型的“Name”和int類型的“Grade”,然后寫一個"Data1”數(shù)據(jù)類,里面存放的使我們的字段類Person數(shù)組:


[System.Serializable]
class Person
{
    public string Name;
    public int Grade;
}
[System.Serializable]
class Data1
{
    public Person[] Person;
}
然后生成Json數(shù)據(jù),將Json數(shù)據(jù)保存到文件中:



using System.IO;
using UnityEngine;

[System.Serializable]
class Person
{
    public string Name;
    public int Grade;
}
[System.Serializable]
class Data
{
    public Person[] Person;
}
public class Demo5 : MonoBehaviour
{
    void Start()
    {
        WriteData();
    }

    //寫數(shù)據(jù)
    public void WriteData()
    {
        //新建一個數(shù)據(jù)類
        Data m_Data = new Data();
        //新建一個字段類 進行賦值
        m_Data.Person = new Person[5];
        for (int i = 0; i < 5; i++)
        {
            Person m_Person = new Person();
            m_Person.Name = "User" + i;
            m_Person.Grade = i + 50;
            m_Data.Person[i] = m_Person;
        }
        //將數(shù)據(jù)轉(zhuǎn)成json
        string js = JsonUtility.ToJson(m_Data);
        //獲取到項目路徑
        string fileUrl = Application.streamingAssetsPath + "\\jsonInfo.txt";
        //打開或者新建文檔
        using (StreamWriter sw =new StreamWriter(fileUrl))
        {
            //保存數(shù)據(jù)
            sw.WriteLine(js);
            //關(guān)閉文檔
            sw.Close();
            sw.Dispose();
        }
    }
}
在這里,我們是新建了兩個數(shù)據(jù)實體類,一個叫Data一個叫Person,它們都有特性:[System.Serializable],也就是序列化,只有加上這個特性,類里面的數(shù)據(jù)才能正常的轉(zhuǎn)成Json數(shù)據(jù)。



讀取Json文件數(shù)據(jù)

讀取數(shù)據(jù),我們使用IO命名空間下的File類的OpenText()函數(shù)進行讀?。?/span>



using System.IO;
using UnityEngine;
public class Demo5 : MonoBehaviour
{
    void Start()
    {
        string jsonData = ReadData();
        Debug.Log(jsonData);
    }
    //讀取文件
    public string ReadData()
    {
        //string類型的數(shù)據(jù)常量
        string readData;
        //獲取到路徑
        string fileUrl = Application.streamingAssetsPath + "\\jsonInfo.json";
        //讀取文件
        using (StreamReader sr =File.OpenText(fileUrl))
        {
            //數(shù)據(jù)保存
            readData = sr.ReadToEnd();
            sr.Close();
        }
        //返回數(shù)據(jù)
        return readData;
    }
}

解析數(shù)據(jù)


using System.IO;
using UnityEngine;

[System.Serializable]
class Person
{
    public string Name;
    public int Grade;
}
[System.Serializable]
class Data
{
    public Person[] Person;
}
public class Demo5 : MonoBehaviour
{
    void Start()
    {
        string jsonData = ReadData();
        Data m_PersonData = JsonUtility.FromJson(jsonData);
        foreach (Person item in m_PersonData.Person)
        {
            Debug.Log(item.Name);
            Debug.Log(item.Grade);
        }
    }

    //讀取文件
    public string ReadData()
    {
        //string類型的數(shù)據(jù)常量
        string readData;
        //獲取到路徑
        string fileUrl = Application.streamingAssetsPath + "\\jsonInfo.json";
        //讀取文件
        using (StreamReader sr =File.OpenText(fileUrl))
        {
            //數(shù)據(jù)保存
            readData = sr.ReadToEnd();
            sr.Close();
        }
        //返回數(shù)據(jù)
        return readData;
    }
}

修改數(shù)據(jù)

using System.IO;
using UnityEngine;

[System.Serializable]
class Person
{
    public string Name;
    public int Grade;
}
[System.Serializable]
class Data
{
    public Person[] Person;
}
public class Demo5 : MonoBehaviour
{
    void Start()
    {
        //讀取數(shù)據(jù)
        string jsonData = ReadData();
        //解析數(shù)據(jù)
        Data m_PersonData = JsonUtility.FromJson(jsonData);
        //找到數(shù)據(jù)修改數(shù)據(jù)
        foreach (Person item in m_PersonData.Person)
        {
            if (item.Name == "User2")
            {
                item.Grade = 58;
            }
        }
        //轉(zhuǎn)成json,然后保存數(shù)據(jù)
        string json = JsonUtility.ToJson(m_PersonData);
        AlterData(json);
    }

    //讀取文件
    public string ReadData()
    {
        //string類型的數(shù)據(jù)常量
        string readData;
        //獲取到路徑
        string fileUrl = Application.streamingAssetsPath + "\\jsonInfo.json";
        //讀取文件
        using (StreamReader sr = File.OpenText(fileUrl))
        {
            //數(shù)據(jù)保存
            readData = sr.ReadToEnd();
            sr.Close();
        }
        //返回數(shù)據(jù)
        return readData;
    }

    //修改數(shù)據(jù)
    public void AlterData(string content)
    {
        //獲取到路徑
        string fileUrl = Application.streamingAssetsPath + "\\jsonInfo.json";
        //讀取文件
        using (StreamWriter sr = new StreamWriter(fileUrl))
        {
            //數(shù)據(jù)保存
            sr.WriteLine(content);
            sr.Close();
        }
    }
}
文章內(nèi)容整理來自網(wǎng)絡(luò),版權(quán)歸原作者所有,如有問題,請聯(lián)系我們!

Copyright 2019 0743119.com

福感科技有限公司 版權(quán)所有 All Rights Reserved

京ICP備20002031號

010-89968230