Json.net反序列化为c#.net 2.0中的对象列表 [英] Json.net deseralize to a list of objects in c# .net 2.0

查看:298
本文介绍了Json.net反序列化为c#.net 2.0中的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些json反序列化到一个集合(列表)中,但是我不确定哪个方法将返回对象列表,还是我必须遍历某些东西并将其复制到我自己的列表中? /p>

谁能告诉我该使用的语法或方法.

我已经创建了带有一些属性的对象,因此可以用来保存数据了. (标题,网址,说明)

我已经尝试过了,但是看起来不太正确

 List<newsItem> test = (List<newsItem>)JsonConvert.DeserializeObject(Fulltext);

解决方案

您是否尝试查看帮助?

http://james.newtonking.com/json/help/?topic = html/SerializingCollections.htm

string json = @"[
  {
    ""Name"": ""Product 1"",
    ""ExpiryDate"": ""\/Date(978048000000)\/"",
    ""Price"": 99.95,
    ""Sizes"": null
  },
  {
    ""Name"": ""Product 2"",
    ""ExpiryDate"": ""\/Date(1248998400000)\/"",
    ""Price"": 12.50,
    ""Sizes"": null
  }
]";

List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);

Console.WriteLine(products.Count);
// 2

Product p1 = products[0];

Console.WriteLine(p1.Name);
// Product 1

I'm trying to deseralize some json into a collection (list), but I'm not sure which method will return a list of objects, or do I have to loop through something and copy it to my own list?

Can anyone tell me the syntax or method I should use for this.

I've created my object with some properties, so it's ready to be used to hold the data. (title,url,description)

I've tried this, but it doesn't seem quite right

 List<newsItem> test = (List<newsItem>)JsonConvert.DeserializeObject(Fulltext);

解决方案

Did you try looking at the help?

http://james.newtonking.com/json/help/?topic=html/SerializingCollections.htm

string json = @"[
  {
    ""Name"": ""Product 1"",
    ""ExpiryDate"": ""\/Date(978048000000)\/"",
    ""Price"": 99.95,
    ""Sizes"": null
  },
  {
    ""Name"": ""Product 2"",
    ""ExpiryDate"": ""\/Date(1248998400000)\/"",
    ""Price"": 12.50,
    ""Sizes"": null
  }
]";

List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);

Console.WriteLine(products.Count);
// 2

Product p1 = products[0];

Console.WriteLine(p1.Name);
// Product 1

这篇关于Json.net反序列化为c#.net 2.0中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆