Deamination JSON file with C #

My problem I have this JSON file:

and I need to save it in the list, but when I try to print the first element of the list, I get a System.ArgumentOutOfRangeException exception, as if my list is empty. this is my code:

JavaScriptSerializer ser = new JavaScriptSerializer(); Causali o = new Causali(); List<CausaliList> lista = new List<CausaliList>(); WebRequest causali = (HttpWebRequest)WebRequest.Create("http://trackrest.cgestmobile.it/causali"); WebResponse risposta = (HttpWebResponse)CreateCausaliRequest(causali).GetResponse(); Stream data = risposta.GetResponseStream(); StreamReader Leggi = new StreamReader(data); string output = Leggi.ReadToEnd(); lista = ser.Deserialize<List<CausaliList>>(output); lst2.Items.Add(lista[0]); 

and these are my two classes to save:

 class Causali { public int id; public string causaliname; public string identificationcode; public string expired; } 

and

 class CausaliList { public Causali causali; } 

Can you help me solve this problem?

+5
source share
2 answers

try this as your root object:

 public class CausaliList { public List<Causali> causali { get; set; } } 

then deserialize your object as follows:

 lista = ser.Deserialize<CausaliList>(output); 

Finally, you can access a list like this:

 lst2.Items.Add(lista.causali[0]); 

Note. I highly recommend using json.NET.

+2
source

To deserialize code in C #: Suppose you have data in var getContent, you can use this:

 dynamic getDesearilize = JsonConvert.DeserializeObject(getContent); 
+2
source

Source: https://habr.com/ru/post/1268585/


All Articles