gpt4 book ai didi

c# - 读入 JSON 数组并转换为 IEnumerable

转载 作者:行者123 更新时间:2023-12-05 01:02:45 25 4
gpt4 key购买 nike

enter image description here

我从未使用过 JSON 文件,但我有实现 JSON 文件的任务,我需要将其转换为 IEnumerable。当我尝试对 JSON 对象进行反序列化时,我得到一个异常,上面写着:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Price_Algorithm.AuctionInfo' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

我的代码:

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)

最佳答案

使用 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 生成的内容中剪切和粘贴。

关于c# - 读入 JSON 数组并转换为 IEnumerable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com