一、讀取JSON
在實(shí)際中,讀取JSON比保存JSON重要得多。因?yàn)榇鏅n、發(fā)送數(shù)據(jù)包往往可以采用其他序列化方法,但游戲的配置文件使用JSON格式比較常見。游戲的配置數(shù)據(jù)不屬于動態(tài)數(shù)據(jù),屬于游戲資源,但很適合用JSON表示。
下面以一個簡單的JSON數(shù)據(jù)文件為例,演示讀取JSON。從整體上看有兩種思路
- 直接整體反序列化為數(shù)據(jù)對象
- 通過寫代碼逐步讀取內(nèi)容
{
"students": [
{
"name": "Alice",
"age": 20,
"major": "Computer Science"
},
{
"name": "Bob",
"age": 22,
"major": "Engineering"
},
{
"name": "Carol",
"age": 21,
"major": "Business"
}
]
}
1、整體反序列化
LitJSON庫支持直接將JSON字符串反序列化為C#對象,但是為了方便使用,最好先準(zhǔn)備一個數(shù)據(jù)結(jié)構(gòu)與JSON完全對應(yīng)的對象。示例如下:
[System.Serializable]
public class Student
{
public string name;
public int age;
public string major;
}
這個類使用了[System.Serializable]
屬性,以便在序列化和反序列化 JSON 數(shù)據(jù)時能夠正確處理。該類有三個屬性,分別表示學(xué)生的姓名(name
)、年齡(age
)和專業(yè)(major
)。
?用LitJson.JsonMapper方法實(shí)現(xiàn)反序列化
using UnityEngine;
using System.Collections.Generic;
using LitJson;
public class JSONDeserializer : MonoBehaviour
{
public TextAsset jsonFile;
void Start()
{
string jsonString = jsonFile.text;
StudentsData data = JsonMapper.ToObject<StudentsData>(jsonString);
List<Student> students = data.students;
// 遍歷學(xué)生列表并輸出信息
foreach (Student student in students)
{
Debug.Log("Name: " + student.name);
Debug.Log("Age: " + student.age);
Debug.Log("Major: " + student.major);
Debug.Log("------------------");
}
}
}
[System.Serializable]
public class StudentsData
{
public List<Student> students;
}
[System.Serializable]
public class Student
{
public string name;
public int age;
public string major;
}
JSON源文件應(yīng)當(dāng)放在Resources/Json文件夾下,將上文的腳本掛載到任意物體上即可進(jìn)行測試,系統(tǒng)會在Console窗口中輸出所有道具的信息。
可以看到,直接序列化對象的優(yōu)點(diǎn)是簡單易行,只要定義好了數(shù)據(jù)類型,就可以直接將JSON轉(zhuǎn)化為方便實(shí)用的對象。但缺點(diǎn)也很明顯,即JSON對數(shù)據(jù)類型的要求十分嚴(yán)格。
2、分步獲取數(shù)據(jù)
下面是分布讀取JSON信息的例子
using UnityEngine;
using System.Collections.Generic;
using LitJson;
public class JSONDeserializer : MonoBehaviour
{
public TextAsset jsonFile;
void Start()
{
string jsonString = jsonFile.text;
JsonData jsonData = JsonMapper.ToObject(jsonString);
// 讀取頂層數(shù)據(jù)對象
string name = (string)jsonData["name"];
int age = (int)jsonData["age"];
string major = (string)jsonData["major"];
Debug.Log("Name: " + name);
Debug.Log("Age: " + age);
Debug.Log("Major: " + major);
Debug.Log("------------------");
// 讀取嵌套對象列表
JsonData studentsData = jsonData["students"];
for (int i = 0; i < studentsData.Count; i++)
{
JsonData studentData = studentsData[i];
string studentName = (string)studentData["name"];
int studentAge = (int)studentData["age"];
string studentMajor = (string)studentData["major"];
Debug.Log("Name: " + studentName);
Debug.Log("Age: " + studentAge);
Debug.Log("Major: " + studentMajor);
Debug.Log("------------------");
}
}
}
這個示例代碼假設(shè) JSON 數(shù)據(jù)文件的頂層結(jié)構(gòu)與上述示例相同。在Start
方法中,我們首先將 JSON 字符串解析為JsonData
對象,然后逐行讀取其中的數(shù)據(jù)。
首先,我們讀取頂層數(shù)據(jù)對象的姓名、年齡和專業(yè),并打印到日志中。然后,我們讀取名為students
的嵌套對象列表,使用循環(huán)迭代每個學(xué)生的數(shù)據(jù)。在每次迭代中,我們讀取學(xué)生對象的姓名、年齡和專業(yè),并打印到日志中。文章來源:http://www.zghlxwxcb.cn/news/detail-736941.html
通過這種方式,你可以逐行讀取 JSON 數(shù)據(jù),并按需處理其中的內(nèi)容。注意要將 JSON 數(shù)據(jù)文件分配給jsonFile
變量,并確保引入了 LitJson 命名空間。文章來源地址http://www.zghlxwxcb.cn/news/detail-736941.html
到了這里,關(guān)于Unity——JSON的讀取的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!