Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Issue with Newtonsoft.Json in Unity

Discussion in 'Scripting' started by TomSa99, Sep 11, 2023.

  1. TomSa99


    Mar 21, 2023
    Hello Unity community,

    I'm encountering a problem while trying to use the Newtonsoft.Json library in Unity. Here's the sequence of events:
    1. I installed the package using NuGet, and it appears correctly in the project references.
    2. I don't receive any errors in Visual Studio, which suggests that the library is correctly referenced there.
    3. However, when I switch back to Unity, I get the following error message: error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
    4. Interestingly, when I double-click on this error message, it takes me back to Visual Studio, and suddenly Newtonsoft.Json disappears from the references.
    5. I manually re-add Newtonsoft.Json to the references in Visual Studio, and the error in Visual Studio disappears. However, the error in Unity persists.
    6. Strangely, every time I re-add the reference in Visual Studio, it doesn't show the little symbol indicating that it needs to be saved. So i attempted to make a small modification to the script in Visual Studio to trigger a save and reload in Unity, but the error CS0246 continues to appear.
    I'm puzzled by this behaviour and suspect it could be a bug. Does anyone have any insights or suggestions for resolving this issue?

    Thank you for your assistance, and if further clarification is needed, please let me know!
  2. spiney199


    Feb 11, 2021
    Visual Studio in the context of Unity should just be regarded as a text-editor. Unity more or less handles the Visual Studio solution and generally overrides anything you do to it.

    Unity has their own Newtonsoft package you can install via the package manager.
  3. CodeSmile


    Apr 10, 2014
    And generally speaking, any package you install via the IDE will not be recognized by the Unity project. Regular NPM packages are installed globally whereas Unity packages or any other asset must be part of the Unity project either located under the Assets tree or installed via Package Manager window.
    tsukimi, TomSa99 and Bunny83 like this.
  4. Kurt-Dekker


    Mar 16, 2013
    Nope, totally normal. As Spiney and Smile point out above, it has to be in Unity or it doesn't count. Unity doesn't care about Visual Studio. Unity is just as happy if you use
    to write your code.

    Here's my notes on getting JSON .NET in your game:

    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as bare arrays, tuples, Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET from the Unity Package Manager (Window -> Package Manager).

    If you want to avoid the Package Mangler's UI, just add this to your

        "com.unity.nuget.newtonsoft-json": "3.0.2",

    Also, always be sure to leverage sites like:
    TomSa99 likes this.
  5. TomSa99


    Mar 21, 2023
    Thank you guys, i really appreciate it! This actually crossed my mind, but since i saw that the Newtonsoft Json.NET is present inside the folder "Packages" in my unity project file i thought i did had to install it with the Unity Package Manager, but i guess that folder is for the scripts and nothing to do with unity. I should have tried that sooner.

    I searched in the Unity Package Manager but didn't find the package there, so i placed a Newtonsoft Json.NET folder, that i downloaded, into the Assets folder in my Unity project and it worked!

    Thank you every one once again!

    So... that didn't work, when i reopened the project in unity i add this errors:
    "Unloading broken assembly Assets/Plugins/Newtonsoft.Json.dll, this assembly can cause crashes in the runtime"
    "Failed to analyze assembly 'Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed': System.TypeLoadException: Could not resolve type with token 01000016 from typeref (expected class 'System.Resources.NeutralResourcesLanguageAttribute' in assembly 'System.Runtime, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')"
    And if i tried to go to play mode it would just crash.
    I did a little search and i found someone that added this "com.unity.nuget.newtonsoft-json": "3.0.1" to the manifest json file inside the Packages folder of the unity project, and this time i have no errors or warnings.
    Still can't do what i need to do but at least i don't have errors anymore!
    Last edited: Sep 11, 2023
    Kurt-Dekker likes this.