asdfohcoibewaib
asdfohcoibewaib

Reputation: 11

C# Read/Write Json Problem with current code

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);
        }
    }
}
}

Upvotes: 0

Views: 1499

Answers (2)

Daruden
Daruden

Reputation: 149

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"]}

Upvotes: 0

user10484617
user10484617

Reputation:

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);
        }
    }
}
}

Upvotes: 2

Related Questions