These days
JSON is Built Into Unity.
https://docs.unity3d.com/Manual/JSONSerialization.html
It's that easy.
A HUGE cause of confusion with Unity is that you get a lot of really old example code on the web.
Ten+ years ago you could use languages other than C# with Unity. Nowadays it is only C#. But you still get 1000s of questions asking why javascript, etc, doesn't work!
Ten+ years ago Unity had a crappy "UI" system. It now has a superb UI system (it's called ".UI"). But you still get many questions about the ridiculous early-days UI system.
In ancient Unity, you had to use pooling in even the simplest cases, say for bullets. Unity nowadays vastly improved performance and pooling is totally unnecessary in normal game situations.
For some reason Unity named the simple timer "Invoke" rather than "Timer" - this has led to 1000s of questions!
Be careful of incredibly out-of-date references to HandyJSON, SuperJSON, MegaJSON, JSONFinallyWithLessBugs, WhoaICanUseJSON and other rubbish packages.
Here is a simple example of parsing some Json from a text file, and in this case putting it into a Dictionary:
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
public class JsonTexts : MonoBehaviour
{
public TextAsset ta; // drag to link in Editor
[NonSerialized] public Dictionary<string, JsonParsePerson> persons;
[Serializable]
public class JsonParsePerson
{
public string id;
public string firstname;
public string lastname;
}
[Serializable]
public class JsonParsePersons
{
public JsonParsePerson[] persons;
}
void Start()
{
JsonParsePersons pp = JsonUtility.FromJson<JsonParsePersons>(ta.text);
persons = pp.persons.ToDictionary(i => i.id, i => i);
// foreach (JsonParsePerson p in pp.persons)
// { Debug.Log($"it worked {p.id} {p.firstname}"); }
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…