[英]Deserializing a JSON file with c#
My problem is i have this JSON file: 我的问题是我有这个JSON文件:
and i have to save it in a list but when i try to print the first element of the list I get a System.ArgumentOutOfRangeException, as if my list is empty. 我必须将其保存在列表中,但是当我尝试打印列表的第一个元素时,我得到一个System.ArgumentOutOfRangeException,就像我的列表为空。 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 class for the saves: 这些是我保存的两个类:
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 it? 你能帮我解决一下吗?
要反序列化c#中的代码:让我们假设您在var getContent中有数据,那么您可以使用:
dynamic getDesearilize = JsonConvert.DeserializeObject(getContent);
please try this as your root object: 请尝试将此作为您的根对象:
public class CausaliList
{
public List<Causali> causali { get; set; }
}
then deserilize your object like this: 然后像这样去除你的对象:
lista = ser.Deserialize<CausaliList>(output);
finally you can access to list like this: 最后你可以像这样访问列表:
lst2.Items.Add(lista.causali[0]);
Note: i strongly recommend to use json.NET. 注意:我强烈建议使用json.NET。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.