读入JSON数组并转换为IEnumerable

时间:2015-01-28 23:59:30

标签: c# json

enter image description here

我从未使用过JSON文件,但我有完成JSON文件的任务,我需要将其转换为IEnumerable。当我尝试对JSON对象进行deserzialize时,我得到一个异常,它说:

  

发生了'Newtonsoft.Json.JsonSerializationException'类型的未处理异常   Newtonsoft.Json.dll

     

其他信息:无法反序列化当前的JSON对象   (例如{“name”:“value”})进入'Price_Algorithm.AuctionInfo'类型   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。

我的代码:

var data = File.ReadAllText(@"C:\benatia.json");
var terry = JsonConvert.DeserializeObject<IEnumerable<AuctionInfo>>(data);

public class AuctionInfo : IEnumerable
{
    public string BidState { get; set; }
    public uint BuyNowPrice { get; set; }
    public uint CurrentBid { get; set; }
    public int Expires { get; set; }
    public ItemData ItemData { get; set; }
    public uint StartingBid { get; set; }
    public string TradeState { get; set; }

    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

我上传了JSON对象,所以每个人都知道整个JSON文件的内容:http://puu.sh/fancH/9a495ecbe9.json(新JSON)

2 个答案:

答案 0 :(得分:3)

使用json2csharp.com您应该反序列化的类应该如下所示:

public class AttributeList
{
    public int Index { get; set; }
    public int Value { get; set; }
}

public class LifeTimeStat
{
    public int Index { get; set; }
    public int Value { get; set; }
}

public class StatsList
{
    public int Index { get; set; }
    public int Value { get; set; }
}

public class ItemData
{
    public int AssetId { get; set; }
    public int Assists { get; set; }
    public List<AttributeList> AttributeList { get; set; }
    public int CardSubTypeId { get; set; }
    public int Contract { get; set; }
    public int DiscardValue { get; set; }
    public int Fitness { get; set; }
    public string Formation { get; set; }
    public object Id { get; set; }
    public int InjuryGames { get; set; }
    public string InjuryType { get; set; }
    public string ItemState { get; set; }
    public string ItemType { get; set; }
    public int LastSalePrice { get; set; }
    public int LeagueId { get; set; }
    public int LifeTimeAssists { get; set; }
    public List<LifeTimeStat> LifeTimeStats { get; set; }
    public int LoyaltyBonus { get; set; }
    public int Morale { get; set; }
    public int Owners { get; set; }
    public int PlayStyle { get; set; }
    public string PreferredPosition { get; set; }
    public int RareFlag { get; set; }
    public int Rating { get; set; }
    public int ResourceId { get; set; }
    public List<StatsList> StatsList { get; set; }
    public int Suspension { get; set; }
    public int TeamId { get; set; }
    public string Timestamp { get; set; }
    public int Training { get; set; }
    public bool Untradeable { get; set; }
    public int Pile { get; set; }
}

public class RootObject
{
    public string BidState { get; set; }
    public int BuyNowPrice { get; set; }
    public int CurrentBid { get; set; }
    public int Expires { get; set; }
    public ItemData ItemData { get; set; }
    public int Offers { get; set; }
    public string SellerEstablished { get; set; }
    public int SellerId { get; set; }
    public string SellerName { get; set; }
    public int StartingBid { get; set; }
    public object TradeId { get; set; }
    public string TradeState { get; set; }
    public object Watched { get; set; }
}

我从您发布的链接中获取了JSON,将其放入文件中,并将一个控制台应用程序放在一起,执行以下操作:

var data = File.ReadAllText("json1.json");
var auctions = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(data);

这是我构建的解决方案http://1drv.ms/1ChmYYx的链接,但它实际上只不过我已发布的内容。

工作得很好,我有26个RootObject个实例。

如果您愿意,可以将RootObject重命名为其他内容。我只是直接从json2charp生成的内容进行剪切和粘贴。

答案 1 :(得分:1)

请改为:

var data = "[" + File.ReadAllText(@"C:\benatia.json") + "]";
var terry = JsonConvert.DeserializeObject<IEnumerable<AuctionInfo>>(data);

您的JSON文件略有格式错误,因为它包含多个以逗号分隔的JSON对象,但没有周围的方括号[ ... ],这将定义JSON数组。

我已将方括号添加到data字符串中。