How to import Newtonsoft.Json in unity3D project

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.

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
insert image description here
. , 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!

Guess you like

Origin blog.csdn.net/qq_41841073/article/details/131669856