unity 读取json文件
程序员文章站
2024-01-20 09:45:40
...
在项目中创建如图所示的文件夹。
myJson中的文件信息
[
{
"name":"aaa中文测试",
"payment":999,
"url":"http://www.bejson.com",
"asdf":"asdf"
},
{
"name":"bbb",
"payment":888,
"url":"http://www.bejson.com",
"asdf":"asdf"
},
{
"name":"ccc中文测试",
"payment":777,
"url":"http://www.bejson.com",
"asdf":"asdf"
},
{
"name":"ddd",
"payment":666,
"url":"http://www.bejson.com",
"asdf":"asdf"
},
{
"name":"eee中文测试",
"payment":555,
"url":"http://www.bejson.com",
"asdf":"asdf"
},
{
"name":"fff",
"payment":444,
"url":"http://www.bejson.com",
"asdf":"asdf"
}
]
创建ReadJson脚本文件,写如下代码。注意这里的str.ToList<JObject>();使用了一个封装json转化的脚本。json转换封装文件JsonHelper
using Newtonsoft.Json.Linq;
using System.IO;
using UnityEngine;
public class ReadJson : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StreamReader sr = new StreamReader(Application.dataPath+ "/Resources/Json/myJson.json");
string str = sr.ReadToEnd();
var jObject = str.ToList<JObject>();
Debug.Log(jObject[0]["name"].ToString());
Debug.Log(jObject.Count);
}
}
上一篇: leetcode第十六天