How to import Newtonsoft.Json in unity3D project
The json interface that comes with the old version of unity is too difficult to use (JsonUtility). It cannot serialize objects such as dictionaries and lists, but can only serialize basic type objects, so it is basically equivalent to nothing.
Click here for the github download address of Newtonsoft.Json-for-Unity-master- Or this link, it is said that after the 2022 version, it has been officially supported by unity
Download the compressed file (.zip) of Newtonsoft.Json-for-Unity-master on git. After decompression, copy it to the Asset/Plugins folder of the unity3D project and use it. You can use Newtonsoft.Json in the script
. , such as the following code:
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string json = JsonConvert.SerializeObject(
new Dictionary<string, string> {
{
"123", "123" },{
"456", "456" }, });
string path = Application.persistentDataPath + "/saveFile.json";
File.WriteAllText(path, json);
}
// Update is called once per frame
void Update()
{
}
}
good luck!