欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity 读取json文件

程序员文章站 2024-01-20 09:45:40
...

在项目中创建如图所示的文件夹。

unity 读取json文件

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);
    }

   
}

 

相关标签: Unity unity json