如何从JSON文件反序列化多个对象

epggiuax  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(139)

我有一个JSON文件,我想用C#把数据取出来。

{
    "Server": [
        {
            "Name": "Server1",
            "AvgTemp": 36,
            "CurTemp": 50.3,
            "MaxTemp": 41,
            "MinTemp": 37
        },
        {
            "Name": "Server2",
            "AvgTemp": 36,
            "CurTemp": 50.3,
            "MaxTemp": 41,
            "MinTemp": 41
        },
        {
            "Name": "Server3",
            "AvgTemp": 36,
            "CurTemp": 50.3,
            "MaxTemp": 41,
            "MinTemp": 36
        },
        {
            "Name": "Server4",
            "AvgTemp": 36,
            "CurTemp": 50.3,
            "MaxTemp": 41,
            "MinTemp": 41.3
        }
    ]
}

我已经试过了:

public ReadJson()
{
    //prevents the threads to block each other while accessing the json file
    using (var file = new FileStream("E:\\Programming\\Timmermann00\\Betrieblicher_Auftrag\\JSON\\Values.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (StreamReader r = new StreamReader(file))
    {
        string json = r.ReadToEnd();
        //Testing
        List<Values> values = JsonConvert.DeserializeObject<List<Values>>(json); 
                
    }
}

public class Values : INotifyPropertyChanged
{
    public Values()
    {
            
    }

    string Name { get; set; }

    string avgTemp; 
    public string AvgTemp { get => avgTemp; set
        {
            if(avgTemp!= value)
            {
                avgTemp = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AvgTemp)));
            }
        }
    }

    string maxTemp; 
    public string MaxTemp { get => maxTemp; set
        {
            if(maxTemp != value)
            {
                maxTemp = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MaxTemp)));
            }
        }
    }

    string minTemp; 
    public string MinTemp
    {
        get => minTemp; set
        {
            if (minTemp != value)
            {
                minTemp= value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MinTemp)));
            }
        }
    }

    string curTemp;
    public string CurTemp
    {
        get => curTemp; set
        {
            if (curTemp != value)
            {
                curTemp = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurTemp)));   
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我收到一个错误消息,指出我的JSON文件中没有任何数组,因此无法转换为List。错误消息:
无法将当前JSON对象(例如{“name”:“value”})反序列化为类型“System.Collections.Generic.List”1[Betrieblicher_Auftrag.Values]“,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
怎么了?

tct7dpnv

tct7dpnv1#

您需要一个Root类来进行反序列化。

public class Root
{
    public List<Value> Server { get; set; }
}

并反序列化为Root示例。

Root root = JsonConvert.DeserializeObject<Root>(json);
List<Values> values = root.Server;
8yparm6h

8yparm6h2#

工作代码。
你能试试这个吗。

public ReadJson()
    {
        //prevents the threads to block each other while accessing the json file
        using (var file = new FileStream("E:\\Programming\\Timmermann00\\Betrieblicher_Auftrag\\JSON\\Values.json", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (StreamReader r = new StreamReader(file))
        {
            string json = r.ReadToEnd();
            //Testing
         var servers = JsonConvert.DeserializeObject<Server>(json);
            List<Values> values = server.Server;
            
        }
    }
}

public List<Values> Server {get; set;}
public class Values:INotifyPropertyChanged
{
    public Values()
    {
        
    }
    
    string Name { get; set; }

    string avgTemp; 
    public string AvgTemp { get => avgTemp; set
        {
            if(avgTemp!= value)
            {
                avgTemp = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AvgTemp)));
            }
        }
    }
    string maxTemp; 
    public string MaxTemp { get => maxTemp; set
        {
            if(maxTemp != value)
            {
                maxTemp = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MaxTemp)));
            }
        }
    }

    string minTemp; 
    public string MinTemp
    {
        get => minTemp; set
        {
            if (minTemp != value)
            {
                minTemp= value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MinTemp)));
            }
        }
    }

    string curTemp;
    public string CurTemp
    {
        get => curTemp; set
        {
            if (curTemp != value)
            {
                curTemp = value;
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurTemp)));   
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

相关问题