在日常开发中会遇到很多读取文件的操作,最常见的类型还是Json、txt、XML、Excel文件,那么今天就分享一下如何读取这几类文件
工程文件已经上传Github,需要的可以自行下载 地址:https://github.com/764424567/Unity_Parse_Json_XML_Excel
我们需要准备json、XML、Excel文件各一个
Json:
{"Data_Class": [{"Id": "1","Name": "tom","Score": "95"},{"Id": "2","Name": "jom","Score": "90"}]}
XML:
<note>
<note1>
<id>1</id>
<name>tom</name>
<score>95</score>
</note1>
<note2>
<id>2</id>
<name>jay</name>
<score>98</score>
</note2>
</note>
Excel:
然后将这几个文件都放到项目的Resources文件夹里,方便读取
记得引入命名空间 using System.IO;
代码:
using System.IO;
using UnityEngine;
public class Parse_Json : MonoBehaviour
{
void Start()
{
ParseJson();
}
public void ParseJson()
{
//获取到Json文件的路径
string filePath = Application.dataPath + "/Resources/test.json";
//string类型的数据常量
string readData = "";
//读取文件
StreamReader str = File.OpenText(filePath);
//数据保存
readData = str.ReadToEnd();
str.Close();
//数据解析并把数据保存到m_PersonData1 变量中
DataClassList dataClass = JsonUtility.FromJson<DataClassList>(readData);
foreach (var item in dataClass.Data_Class)
{
Debug.Log(item.Id);
Debug.Log(item.Name);
Debug.Log(item.Score);
}
}
}
[System.Serializable]
public class Data_Class
{
public string Id;
public string Name;
public string Score;
}
[System.Serializable]
public class DataClassList
{
public Data_Class[] Data_Class;
}
这是一个数据类,用来接收解析过来的Json数据
效果:
记得引入命名空间: using System.IO; using System.Xml;
代码:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class Parse_Xml : MonoBehaviour
{
void Start()
{
ParseXML();
}
public void ParseXML()
{
//获取到XML文件的路径
string filePath = Application.dataPath + "/Resources/test.xml";
if (File.Exists(filePath))
{
//创建一个数据类集合
List<Data_Class> dataList = new List<Data_Class>();
//创建一个XML文件解析对象
XmlDocument xmlDoc = new XmlDocument();
//加载XML
xmlDoc.Load(filePath);
//获取根节点
XmlNode rootNode = xmlDoc.FirstChild;
//获取根节点下面所有的子节点
XmlNodeList nodeList = rootNode.ChildNodes;
//遍历节点
foreach (XmlElement item in nodeList)
{
Data_Class dataClass = new Data_Class();
dataClass.Id = item.ChildNodes.Item(0).InnerText;
dataClass.Name = item.ChildNodes.Item(1).InnerText;
dataClass.Score = item.ChildNodes.Item(2).InnerText;
dataList.Add(dataClass);
}
//打印数据
foreach (Data_Class item in dataList)
{
Debug.Log(item.Id);
Debug.Log(item.Name);
Debug.Log(item.Score);
}
}
}
}
数据类:
[System.Serializable]
public class Data_Class
{
public string Id;
public string Name;
public string Score;
}
效果:
这个比较麻烦的是dll的引入:
这三个dll的链接为: https://download.csdn.net/download/q764424567/12397475
按需下载
引入命名空间: using Excel; using System.IO; using System.Data;
代码:
using UnityEngine;
using System.Collections.Generic;
using Excel;
using System.IO;
using System.Data;
public class Parse_Excel : MonoBehaviour
{
void Start()
{
ParseExcel();
}
public void ParseExcel()
{
//获取到Json文件的路径
string filePath = Application.dataPath + "/Resources/test.xlsx";
FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
IExcelDataReader iExcelDR = ExcelReaderFactory.CreateOpenXmlReader(fs);
DataSet ds = iExcelDR.AsDataSet();
List<TableData> data = new List<TableData>();
int columns = ds.Tables[0].Columns.Count; //总列数,Tables[0]为表1
int rows = ds.Tables[0].Rows.Count; //总行数
for (int i = 1; i < rows; i++)
{
TableData td = new TableData();
td.Id = ds.Tables[0].Rows[i][0].ToString();//ds.Tables[0].Rows[i][0]是Object,需强行转换为String型
td.Name = ds.Tables[0].Rows[i][1].ToString();
td.Score = ds.Tables[0].Rows[i][2].ToString();
Debug.Log(td.Id + " " + td.Name + " " + td.Score);
data.Add(td);
}
}
}
效果: