Click here to Skip to main content
15,882,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, guys. My first time asking here. So, I got a situation trying to get some information from a Json File. I want to reach the information that is inside of the Public Class Valore and Resultados. This is how my code looks like:
public class Valore
        public string fecha { get; set; }
    public string hora { get; set; }
    public string pml { get; set; }
    public string pml_ene { get; set; }
    public string pml_per { get; set; }
    public string pml_cng { get; set; }

public class Resultado
    public string clv_nodo { get; set; }
    public List<valore> Valores { get; set; }

public class RootObject
    public string nombre { get; set; }
    public string proceso { get; set; }
    public string sistema { get; set; }
    public string area { get; set; }
    public List<resultado> Resultados { get; set; }
    public string status { get; set; }

class Program
    static void Main(string[] args)
        RootObject PrecioMarginal;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"Someplaceoninternet");
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
            string json = reader.ReadToEnd();
            price = JsonConvert.DeserializeObject<rootobject>(json);
        Console.WriteLine("El estatus del nodo es: " + Price.status);

What I have tried:

Not too much, my first time making code with C#
Updated 8-Feb-18 20:47pm
David_Wimbley 9-Feb-18 0:06am    
Do you happen to have a sample of the JSON you are attempting to deserialize?
Graeme_Grant 9-Feb-18 0:10am    
Having problems posting solutions, so will post answer here...

Solutions now posting..

This question gets asked a lot in here, so I published an article that answers yours and other questions: Working with JSON in C# & VB[^]
Share this answer
I can strongly recommend using Newtonsoft.JSON for deserialization:
/// <summary>
/// Load all known sheets
/// </summary>
/// <param name="path">
/// Full path to the JSON file containing the sheet data.
/// If not provided, the default will be used.</param>
/// <returns></returns>
public static List<AveryLabelSheet> Load(string path = null)
    if (path == null) path = GetSensiblePathToSheetDataFile();
    if (!File.Exists(path)) throw new FileNotFoundException("Cannot locate the Sheet Data file:\n   \"" + path + "\"\nPlease check the path and try again.");
        all = JsonConvert.DeserializeObject<List<AveryLabelSheet>>(File.ReadAllText(path));
    catch (Exception ex)
        throw new FileLoadException("Unable to load Sheet Data.\nThe file \"" + path + "\" does not load correctly.", ex);
    isLoaded = true;
    return new List<AveryLabelSheet>(all);
One line of code does the whole job:
all = JsonConvert.DeserializeObject<List<AveryLabelSheet>>

You will need to install Newtonsoft.JSON: you can add it to your project via the NuGet Package Manager (Tools ... NuGet Package Manager ... Package Manager Console):
PM> Install-Package Newtonsoft.Json

As an aside, I'm using JSON data for the first time with a recent prioject, and it's remarkably easy with the Newtonsoft stuff: serialize and deserialize a whole list of a custom class in one line of code each! The only hassle I found was it only works if the relevant class properties have both public getter and setter - I originally used private setters, and the data failed to read back, understandably.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900