0

Error: error CS0103: The name 'JsonConvert' does not exist in the current context

I have the latest Newtonsoft download and I cant figure out any way to fix this I've gone over about 50 different links all saying to install it. Which it is.

Am I missing something small? I am just trying to add my json elements to a list so I can use them in a program and then write back to the Json file. If this cant work can someone link or show me another way to do this in c#?

using System.IO;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

namespace Newtonsoft.Json {
public class CustomControls : MonoBehaviour
{
    private List<string> controls;

    //public object JsonConvert { get; private set; }

    // Start is called before the first frame update
    void Start()
    {
        LoadJson();
        for (int i = 0; i < controls.Count; i++)
        {
            Debug.Log(controls[i].ToString());
        }
    }

    public void LoadJson()
    {
        using (StreamReader r = new StreamReader("CustomControls.json"))
        {
            string json = r.ReadToEnd();
            controls = JsonConvert.DeserializeObject<List<string>>(json);
        }
    }
}
}
2
  • 1
    Have you added a reference to the Newtonsoft assemblies in your project? Commented May 6, 2019 at 0:15
  • Try changing the namespace to something other than Newtonsoft.Json Commented May 6, 2019 at 0:16

2 Answers 2

2

Change the name of the namespace in your code

using System.IO;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;

namespace AnythingOtherThanNewtonsoft.Json {
public class CustomControls : MonoBehaviour
{
    private List<string> controls;

    //public object JsonConvert { get; private set; }

    // Start is called before the first frame update
    void Start()
    {
        LoadJson();
        for (int i = 0; i < controls.Count; i++)
        {
            Debug.Log(controls[i].ToString());
        }
    }

    public void LoadJson()
    {
        using (StreamReader r = new StreamReader("CustomControls.json"))
        {
            string json = r.ReadToEnd();
            controls = JsonConvert.DeserializeObject<List<string>>(json);
        }
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want or need to use Newtonsoft.Json for some reason, I highly recommend you this guide.

Else, here is an alternative for your case, in which you can use unity`s JsonUtility.

You only need to wrap it in a class

using System.IO;
using UnityEngine;
using System.Collections.Generic;

public class MyControls{
    public List<string> controls;
}

public class CustomControls : MonoBehaviour
{
    private MyControls myControls;

    void Start()
    {
        SaveJson();
        LoadJson();
        for (int i = 0; i < myControls.controls.Count; i++)
        {
            Debug.Log(myControls.controls[i]);//No need to use ToString() here, it`s already a string
        }
    }

    public void SaveJson()
    {
        MyControls testControls = new MyControls() //Creates a new instance of MyControls
        {
            controls = new List<string>(2) {"a", "b"} //Your list of strings should go here.
        };

        File.WriteAllText("CustomControls.json", JsonUtility.ToJson(testControls)); 

    }

    public void LoadJson()
    {
        using (StreamReader r = new StreamReader("CustomControls.json"))
        {
            string json = r.ReadToEnd();
            myControls = JsonUtility.FromJson<MyControls>(json);
        }
    }
}

Edit: Added an example on how to save a json file using JsonUtility The file created have this on it {"controls":["a","b"]}

8 Comments

This doesnt add anything to the myControls.controls
Did you try renaming your NameSpace
what do you mean by that, I deleted everything and copied and pasted to see if it would work, am I supposed to use a namespace with this like my code above?
it's very simple. Go to your original code where you have namespace Newtonsoft.json and change it to something else. The compiler is confused, because Newtonsoft already uses that namespace.
I tried with no namespace and with namespace System the count still is 0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.