2

So I have this JSON file. I want to get the data out of it in 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
        }
    ]
}

I already tried this:

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

I get an error that says that my JSON file doesn't have any arrays in it so it can't convert into a List. Error message:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Betrieblicher_Auftrag.Values]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

What is wrong?

2 Answers 2

2

You need a Root class for the deserialization.

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

And deserialize as Root instance.

Root root = JsonConvert.DeserializeObject<Root>(json);
List<Values> values = root.Server; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the fast help. It worked. Can you explain me what the exact diffrence is between you "Root" class an my way to convert it directly into an list?
Because your JSON is an object which contains property that holds the array with List<Values> type. You should deserialize the JSON as Root object first and next retrieve the array.
1

Working code.

can you try with this.

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

Comments

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.