Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#当前代码存在读/写Json问题_C#_Json_List_Unity3d - Fatal编程技术网

C#当前代码存在读/写Json问题

C#当前代码存在读/写Json问题,c#,json,list,unity3d,C#,Json,List,Unity3d,错误:错误CS0103:名称“JsonConvert”在当前上下文中不存在 我有最新的Newtonsoft下载,我想不出任何方法来解决这个问题。我已经浏览了大约50个不同的链接,都说要安装它。就是这样 我是不是错过了一些小东西?我只是尝试将json元素添加到列表中,以便在程序中使用它们,然后写回json文件。如果这不能工作,有人可以链接或告诉我另一种方法在c#做这件事吗 使用System.IO; 使用System.Collections.Generic; 使用UnityEngine; 使用New

错误:错误CS0103:名称“JsonConvert”在当前上下文中不存在

我有最新的Newtonsoft下载,我想不出任何方法来解决这个问题。我已经浏览了大约50个不同的链接,都说要安装它。就是这样

我是不是错过了一些小东西?我只是尝试将json元素添加到列表中,以便在程序中使用它们,然后写回json文件。如果这不能工作,有人可以链接或告诉我另一种方法在c#做这件事吗

使用System.IO;
使用System.Collections.Generic;
使用UnityEngine;
使用Newtonsoft.Json;
名称空间Newtonsoft.Json{
公共类自定义控件:单行为
{
私人名单管制;
//公共对象JsonConvert{get;private set;}
//在第一帧更新之前调用Start
void Start()
{
LoadJson();
for(int i=0;i
如果您出于某种原因想要或需要使用Newtonsoft.Json,我强烈建议您使用此选项

除此之外,这里还有另一种情况,您可以使用unity的JsonUtility

你只需要把它包装在一个类中

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);
        }
    }
}
使用System.IO;
使用UnityEngine;
使用System.Collections.Generic;
公共类真菌控制{
公开名单管制;
}
公共类自定义控件:单行为
{
私人真菌控制真菌控制;
void Start()
{
SaveJson();
LoadJson();
对于(int i=0;i
编辑:添加了一个关于如何使用JsonUtility保存json文件的示例
创建的文件上有以下内容:[“a”,“b”]}

如果出于某种原因需要使用Newtonsoft.Json,我强烈建议您这样做

除此之外,这里还有另一种情况,您可以使用unity的JsonUtility

你只需要把它包装在一个类中

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);
        }
    }
}
使用System.IO;
使用UnityEngine;
使用System.Collections.Generic;
公共类真菌控制{
公开名单管制;
}
公共类自定义控件:单行为
{
私人真菌控制真菌控制;
void Start()
{
SaveJson();
LoadJson();
对于(int i=0;i
编辑:添加了一个关于如何使用JsonUtility保存json文件的示例
创建的文件上有以下内容
{“controls”:[“a”,“b”]}

更改代码中命名空间的名称

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);
        }
    }
}
}
使用System.IO;
使用System.Collections.Generic;
使用UnityEngine;
使用Newtonsoft.Json;
命名空间anythingotherthnewtonsoft.Json{
公共类自定义控件:单行为
{
私人名单管制;
//公共对象JsonConvert{get;private set;}
//在第一帧更新之前调用Start
void Start()
{
LoadJson();
for(int i=0;i
更改代码中命名空间的名称

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);
        }
    }
}
}
使用System.IO;
使用System.Collections.Generic;
使用UnityEngine;
使用Newtonsoft.Json;
命名空间anythingotherthnewtonsoft.Json{
公共类自定义控件:单行为
{
私人名单管制;
//公共对象JsonConvert{get;private set;}
//在第一帧更新之前调用Start
void Start()
{
LoadJson();
for(int i=0;i
是否在项目中添加了对Newtonsoft程序集的引用?是否尝试将命名空间更改为Newtonsoft.json以外的其他名称?是否在项目中添加了对Newtonsoft程序集的引用?是否尝试将命名空间更改为Newtonsoft.json以外的其他名称?这不会向MyControl.ControlsId添加任何内容重命名你的名称空间你觉得我怎么样