原文内容来自:Read from and write to external .json files using JsonUtilities or LitJson
文件Unity 版本号5.3,使用时候Unity 版本号5.6
文件分流unity-json-master本文仅作分析,学习用途。
类似下面这样,//* 这样的表示和 LitJson 对应 的代码 相同。
//* 的 分数 +1
通过注释来区别 。主要用于 表示 在 代码中 可以 改变 的数据。
someList.Add (createSubObject ("Amazing Angus6", 64546));
原代码,无注释。
someList.Add (createSubObject ("Amazing Angus", 6454));
_2ReadJson_JsonUtility
----------------------------------------------------------------------------------------------原代码+注释
// Read JSON File and transform the string to objects
// Only works from Unity 5.3 on with its new JSONUtility class.
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
public class _2ReadJson_JsonUtility : MonoBehaviour{
private void Start (){
/**
* 1. Fetch JSON-formatted string from text file * 从 文本文件中提取 JSON格式 字符串
*/
// string jsonString = "{ \"name\": \"Fabi\", \"level\": \"4711\", \"tags\": [\"Beginner\",\"Fast\"] }";
string jsonString = File.ReadAllText (Application.dataPath + "/Resources/Json_basic.json");
// Debug.Log(Application.dataPath);
//* 定义 一个字符串 jsonString
//* 读取 路径Application.dataPath + "/Resources/Json_basic.json" 的.json文件
/**
* 2. Transform JSON-formatted text into object
*/
//* 转换 JSON格式 ,使得 jsonString字符串变量 转换 为JSON格式
ObjectData myObject = JsonUtility.FromJson<ObjectData> (jsonString);
Debug.Log ("Whole JSON String: "+jsonString);
foreach (string tag in myObject.tags)
{//* 遍历 每一个 在字符串List 的tagsList 的 tags,显示并且输出字符串
Debug.Log ("Print List Item: " + tag); // logs Beginner, then Fast
}
}
}
----------------------------------------------------------------------------------------------代码图片
----------------------------------------------------------------------------------------------代码运行的结果
----------------------------------------------------------------------------------------------逻辑代码图