Reputation: 18544
I've never worked with JSON files but I have the task to implement the JSON file and I need to convert it to a IEnumerable. When I try to deserzialize the JSON object I get an exception which says:
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.
My code:
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();
}
}
I've uploaded the JSON object so everyone knows the content of the whole JSON file: http://puu.sh/fancH/9a495ecbe9.json (new JSON)
Upvotes: 4
Views: 21461
Reputation: 18165
Using json2csharp.com the classes you should be deserializing into should look like this:
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; }
}
I took the JSON from the link you posted, threw it into a file, and put together a little console application that does the following:
var data = File.ReadAllText("json1.json");
var auctions = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(data);
Here's a link to the solution I built http://1drv.ms/1ChmYYx but it's really no more than what I already posted.
Worked perfectly, I got 26 instances of RootObject
.
If you want you can rename RootObject
to something else. I just cut-and-pasted directly from what json2charp generated.
Upvotes: 7
Reputation: 79581
Do this instead:
var data = "[" + File.ReadAllText(@"C:\benatia.json") + "]";
var terry = JsonConvert.DeserializeObject<IEnumerable<AuctionInfo>>(data);
Your JSON file is slightly malformed, since it contains multiple JSON objects separated by commas, but no surrounding square brackets [
... ]
, which would define a JSON array.
I have added the square brackets to your data
string.
Upvotes: 3