NDV NDV - 3 months ago 40
C# Question

json.net deserialize a json object in c#

I've a problem by deserialize a json object.

Explicitly this: http://store.steampowered.com/api/appdetails?appids=730

I get the following error: System.NullReferenceException on the following code:

string json = new WebClient().DownloadString("http://store.steampowered.com/api/appdetails?appids=730");


More informations:


Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Steam.Program+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.


But I think this is a mistake from Visual Studio.

Classes:

public class PcRequirements
{
public string minimum { get; set; }
}

public class MacRequirements
{
public string minimum { get; set; }
}

public class LinuxRequirements
{
public string minimum { get; set; }
}

public class PriceOverview
{
public string currency { get; set; }
public int initial { get; set; }
public int final { get; set; }
public int discount_percent { get; set; }
}

public class Sub
{
public int packageid { get; set; }
public string percent_savings_text { get; set; }
public int percent_savings { get; set; }
public string option_text { get; set; }
public string option_description { get; set; }
public string can_get_free_license { get; set; }
public bool is_free_license { get; set; }
public int price_in_cents_with_discount { get; set; }
}

public class PackageGroup
{
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public string selection_text { get; set; }
public string save_text { get; set; }
public int display_type { get; set; }
public string is_recurring_subscription { get; set; }
public List<Sub> subs { get; set; }
}

public class Platforms
{
public bool windows { get; set; }
public bool mac { get; set; }
public bool linux { get; set; }
}

public class Metacritic
{
public int score { get; set; }
public string url { get; set; }
}

public class Category
{
public int id { get; set; }
public string description { get; set; }
}

public class Genre
{
public string id { get; set; }
public string description { get; set; }
}

public class Screenshot
{
public int id { get; set; }
public string path_thumbnail { get; set; }
public string path_full { get; set; }
}

public class Webm
{
public string __invalid_name__480 { get; set; }
public string max { get; set; }
}

public class Movie
{
public int id { get; set; }
public string name { get; set; }
public string thumbnail { get; set; }
public Webm webm { get; set; }
public bool highlight { get; set; }
}

public class Recommendations
{
public int total { get; set; }
}

public class Highlighted
{
public string name { get; set; }
public string path { get; set; }
}

public class Achievements
{
public int total { get; set; }
public List<Highlighted> highlighted { get; set; }
}

public class ReleaseDate
{
public bool coming_soon { get; set; }
public string date { get; set; }
}

public class SupportInfo
{
public string url { get; set; }
public string email { get; set; }
}

public class Data
{
public string type { get; set; }
public string name { get; set; }
public int steam_appid { get; set; }
public int required_age { get; set; }
public bool is_free { get; set; }
public string controller_support { get; set; }
public string detailed_description { get; set; }
public string about_the_game { get; set; }
public string supported_languages { get; set; }
public string header_image { get; set; }
public string website { get; set; }
public PcRequirements pc_requirements { get; set; }
public MacRequirements mac_requirements { get; set; }
public LinuxRequirements linux_requirements { get; set; }
public List<string> developers { get; set; }
public List<string> publishers { get; set; }
public PriceOverview price_overview { get; set; }
public List<int> packages { get; set; }
public List<PackageGroup> package_groups { get; set; }
public Platforms platforms { get; set; }
public Metacritic metacritic { get; set; }
public List<Category> categories { get; set; }
public List<Genre> genres { get; set; }
public List<Screenshot> screenshots { get; set; }
public List<Movie> movies { get; set; }
public Recommendations recommendations { get; set; }
public Achievements achievements { get; set; }
public ReleaseDate release_date { get; set; }
public SupportInfo support_info { get; set; }
public string background { get; set; }
}

public class steamstore
{
public bool success { get; set; }
public Data data { get; set; }
}

public class RootObject
{
public steamstore steamstore { get; set; }
}


Main:

static void Main(string[] args)
{
string json = new WebClient().DownloadString("http://store.steampowered.com/api/appdetails?appids=730");
var root = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(root.steamstore.data.name);
Console.ReadKey();
}



Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Steam.Program+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Bob Bob
Answer

Thats because the json does not match

Extract 730 object then parse it into your defined classes

public static void Main()
{
    string json = new WebClient().DownloadString("http://store.steampowered.com/api/appdetails?appids=730");
    JObject jObject = JObject.Parse(json);
    var root = jObject["730"].Value<JObject>().ToObject<steamstore>();
    Console.WriteLine(root.data.name);
    Console.ReadKey();
}

Here is the Json from http://store.steampowered.com/api/appdetails?appids=730

{"730":{"success":true,"data":{"type":"game","name":"Counter-Strike: Global Offensive","steam_appid":730,"required_age":0,"is_free":false,"controller_support":"full","detailed_description":"Counter-Strike: Global Offensive (CS: GO) will expand upon the team-based action gameplay that it pioneered when it was launched 14 years ago.<br \/>\r\n<br \/>\r\nCS: GO features new maps, characters, and weapons and delivers updated versions of the classic CS content (de_dust, etc.). In addition, CS: GO will introduce new gameplay modes, matchmaking, leader boards, and more.<br \/>\r\n<br \/>\r\n&quot;Counter-Strike took the gaming industry by surprise when the unlikely MOD became the most played online PC action game in the world almost immediately after its release in August 1999,&quot; said Doug Lombardi at Valve. &quot;For the past 12 years, it has continued to be one of the most-played games in the world, headline competitive gaming tournaments and selling over 25 million units worldwide across the franchise. CS: GO promises to expand on CS' award-winning gameplay and deliver it to gamers on the PC as well as the next gen consoles and the Mac.&quot;","about_the_game":"Counter-Strike: Global Offensive (CS: GO) will expand upon the team-based action gameplay that it pioneered when it was launched 14 years ago.<br \/>\r\n<br \/>\r\nCS: GO features new maps, characters, and weapons and delivers updated versions of the classic CS content (de_dust, etc.). In addition, CS: GO will introduce new gameplay modes, matchmaking, leader boards, and more.<br \/>\r\n<br \/>\r\n&quot;Counter-Strike took the gaming industry by surprise when the unlikely MOD became the most played online PC action game in the world almost immediately after its release in August 1999,&quot; said Doug Lombardi at Valve. &quot;For the past 12 years, it has continued to be one of the most-played games in the world, headline competitive gaming tournaments and selling over 25 million units worldwide across the franchise. CS: GO promises to expand on CS' award-winning gameplay and deliver it to gamers on the PC as well as the next gen consoles and the Mac.&quot;","supported_languages":"Czech, Danish, Dutch, English<strong>*<\/strong>, Finnish, French, German, Hungarian, Italian, Japanese, Korean, Norwegian, Polish, Portuguese, Portuguese-Brazil, Romanian, Russian, Simplified Chinese, Spanish, Swedish, Thai, Traditional Chinese, Turkish, Bulgarian, Ukrainian<br><strong>*<\/strong>languages with full audio support","header_image":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/header.jpg?t=1467065027","website":"http:\/\/blog.counter-strike.net\/","pc_requirements":{"minimum":"<strong>Minimum:<\/strong>\r<br><ul class=\"bb_ul\"><li><strong>OS:<\/strong> Windows\u00ae 7\/Vista\/XP\r<br><\/li><li><strong>Processor:<\/strong> Intel\u00ae Core\u2122 2 Duo E6600 or AMD Phenom\u2122 X3 8750 processor or better\r<br><\/li><li><strong>Memory:<\/strong> 2 GB RAM\r<br><\/li><li><strong>Graphics:<\/strong> Video card must be 256 MB or more and should be a DirectX 9-compatible with support for Pixel Shader 3.0\r<br><\/li><li><strong>DirectX:<\/strong> Version 9.0c\r<br><\/li><li><strong>Storage:<\/strong> 8 GB available space\r<\/li><\/ul>"},"mac_requirements":{"minimum":"<strong>Minimum:<\/strong>\r<br><ul class=\"bb_ul\"><li><strong>OS:<\/strong> MacOS X 10.6.6 or higher\r<br><\/li><li><strong>Processor:<\/strong> Intel Core Duo Processor (2GHz or better)\r<br><\/li><li><strong>Memory:<\/strong> 2 GB RAM\r<br><\/li><li><strong>Graphics:<\/strong> ATI Radeon HD 2400 or better \/ NVidia 8600M or better\r<br><\/li><li><strong>Storage:<\/strong> 8 GB available space\r<\/li><\/ul>"},"linux_requirements":{"minimum":"<strong>Minimum:<\/strong>\r<br><ul class=\"bb_ul\"><li><strong>OS:<\/strong> Ubuntu 12.04\r<br><\/li><li><strong>Processor:<\/strong> 64-bit Dual core from Intel or AMD at 2.8 GHz\r<br><\/li><li><strong>Memory:<\/strong> 4 GB RAM\r<br><\/li><li><strong>Graphics:<\/strong> nVidia GeForce 8600\/9600GT, ATI\/AMD Radeon HD2600\/3600 (Graphic Drivers: nVidia 310, AMD 12.11), OpenGL 2.1\r<br><\/li><li><strong>Storage:<\/strong> 8 GB available space\r<br><\/li><li><strong>Sound Card:<\/strong> OpenAL Compatible Sound Card\r<\/li><\/ul>"},"developers":["Valve"],"publishers":["Valve"],"price_overview":{"currency":"SGD","initial":1500,"final":1500,"discount_percent":0},"packages":[54029],"package_groups":[{"name":"default","title":"Buy Counter-Strike: Global Offensive","description":"","selection_text":"Select a purchase option","save_text":"","display_type":0,"is_recurring_subscription":"false","subs":[{"packageid":54029,"percent_savings_text":"","percent_savings":0,"option_text":"Counter-Strike: Global Offensive - S$15.00","option_description":"","can_get_free_license":"0","is_free_license":false,"price_in_cents_with_discount":1500}]}],"platforms":{"windows":true,"mac":true,"linux":true},"metacritic":{"score":83,"url":"http:\/\/www.metacritic.com\/game\/pc\/counter-strike-global-offensive?ftag=MCD-06-10aaa1f"},"categories":[{"id":1,"description":"Multi-player"},{"id":22,"description":"Steam Achievements"},{"id":28,"description":"Full controller support"},{"id":29,"description":"Steam Trading Cards"},{"id":30,"description":"Steam Workshop"},{"id":35,"description":"In-App Purchases"},{"id":8,"description":"Valve Anti-Cheat enabled"},{"id":15,"description":"Stats"}],"genres":[{"id":"1","description":"Action"}],"screenshots":[{"id":0,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_34090867f1a02b6c17652ba9043e3f622ed985a9.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_34090867f1a02b6c17652ba9043e3f622ed985a9.1920x1080.jpg?t=1467065027"},{"id":1,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_1d30c9a215fd621e2fd74f40d93b71587bf6409c.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_1d30c9a215fd621e2fd74f40d93b71587bf6409c.1920x1080.jpg?t=1467065027"},{"id":2,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_baa02e979cd3852e3c4182afcd603ab64e3502f9.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_baa02e979cd3852e3c4182afcd603ab64e3502f9.1920x1080.jpg?t=1467065027"},{"id":3,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_ffe584c163a2b16e9c1b733b1c8e2ba669fb1204.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_ffe584c163a2b16e9c1b733b1c8e2ba669fb1204.1920x1080.jpg?t=1467065027"},{"id":4,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_d87c102d028d545c877363166c9d8377014f0c23.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_d87c102d028d545c877363166c9d8377014f0c23.1920x1080.jpg?t=1467065027"},{"id":5,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_9d0735a5fbe523fd39f2c69c047019843c326cea.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_9d0735a5fbe523fd39f2c69c047019843c326cea.1920x1080.jpg?t=1467065027"},{"id":6,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_9d889bec419cf38910ccf72dd80f9260227408ee.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_9d889bec419cf38910ccf72dd80f9260227408ee.1920x1080.jpg?t=1467065027"},{"id":7,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_ccc4ce6edd4c454b6ce7b0757e633b63aa93921d.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_ccc4ce6edd4c454b6ce7b0757e633b63aa93921d.1920x1080.jpg?t=1467065027"},{"id":8,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_9db552fd461722f1569e3292d8f2ea654c8ffdef.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_9db552fd461722f1569e3292d8f2ea654c8ffdef.1920x1080.jpg?t=1467065027"},{"id":9,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_74c1a0264ceaf57e5fb51d978205045223b48a18.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_74c1a0264ceaf57e5fb51d978205045223b48a18.1920x1080.jpg?t=1467065027"},{"id":10,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_7eaa83e44f5218a7bf5f88a0c750e36052e31d7d.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_7eaa83e44f5218a7bf5f88a0c750e36052e31d7d.1920x1080.jpg?t=1467065027"},{"id":11,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_68007896ad6071b7062bac530c481e097105efc0.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_68007896ad6071b7062bac530c481e097105efc0.1920x1080.jpg?t=1467065027"},{"id":12,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_2fcee01bace72bc47a2ad0ba82620588239e93df.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_2fcee01bace72bc47a2ad0ba82620588239e93df.1920x1080.jpg?t=1467065027"},{"id":13,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_f5875f8de419a3d5133ae7245b8296db2c027dd8.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_f5875f8de419a3d5133ae7245b8296db2c027dd8.1920x1080.jpg?t=1467065027"},{"id":14,"path_thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_36f82c71ee2180159b060b155bf3d06dd8167327.600x338.jpg?t=1467065027","path_full":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/ss_36f82c71ee2180159b060b155bf3d06dd8167327.1920x1080.jpg?t=1467065027"}],"movies":[{"id":81958,"name":"CS:GO Trailer Long","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/81958\/movie.293x165.jpg?t=1459467902","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/81958\/movie480.webm?t=1459467902","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/81958\/movie_max.webm?t=1459467902"},"highlight":true},{"id":2028288,"name":"CS: GO Pro Tip Series: TM","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028288\/movie.293x165.jpg?t=1459467945","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028288\/movie480.webm?t=1459467945","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028288\/movie_max.webm?t=1459467945"},"highlight":false},{"id":2028287,"name":"CS: GO Pro Tip Series: sapphiRe","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028287\/movie.293x165.jpg?t=1459467936","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028287\/movie480.webm?t=1459467936","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028287\/movie_max.webm?t=1459467936"},"highlight":false},{"id":2028286,"name":"CS: GO Pro Tip Series: AZK","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028286\/movie.293x165.jpg?t=1459467927","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028286\/movie480.webm?t=1459467927","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028286\/movie_max.webm?t=1459467927"},"highlight":false},{"id":2028285,"name":"CS: GO Pro Tip Series: Fifflaren","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028285\/movie.293x165.jpg?t=1459467962","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028285\/movie480.webm?t=1459467962","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028285\/movie_max.webm?t=1459467962"},"highlight":false},{"id":2028284,"name":"CS: GO Pro Tip Series: ruggah","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028284\/movie.293x165.jpg?t=1459467919","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028284\/movie480.webm?t=1459467919","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028284\/movie_max.webm?t=1459467919"},"highlight":false},{"id":2028283,"name":"CS: GO Pro Tip Series: nEiLZiNHo","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028283\/movie.293x165.jpg?t=1459467911","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028283\/movie480.webm?t=1459467911","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028283\/movie_max.webm?t=1459467911"},"highlight":false},{"id":2028289,"name":"CS: GO Pro Tip Series: Semphis","thumbnail":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028289\/movie.jpg?t=1459467954","webm":{"480":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028289\/movie480.webm?t=1459467954","max":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2028289\/movie_max.webm?t=1459467954"},"highlight":false}],"recommendations":{"total":1385472},"achievements":{"total":167,"highlighted":[{"name":"Someone Set Up Us The Bomb","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/9f60ea3c56b4ab248ab598bbd62568b953116301.jpg"},{"name":"Boomala Boomala","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/325ccbd68e599083c1597fd90dc6d4265d1ca3b4.jpg"},{"name":"The Hurt Blocker","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/9b8627953feca51a56f0c2ac871493c8634a1c07.jpg"},{"name":"Body Bagger","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/b11ef0453168cd3d10684e184004f71dcc0faa82.jpg"},{"name":"Corpseman","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/60d83a42df84fa5d84910af681370579e3bb16bf.jpg"},{"name":"God of War","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/2c2bb56a4f7376b53eeaf8e6d84044460b0d1cf2.jpg"},{"name":"Second to None","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/986a53e24114699cb1f230f52b55b27e0de28ecf.jpg"},{"name":"Counter-Counter-Terrorist","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/c09cb3fe34841fd1000a48c3b7825c4fde026188.jpg"},{"name":"Rite of First Defusal","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/b2027dac5a9883f0f145e78a33e531a58944f3ec.jpg"},{"name":"Short Fuse","path":"http:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/apps\/730\/648550738f4845f12aa686a1b4ee8c4a51ec2348.jpg"}]},"release_date":{"coming_soon":false,"date":"22 Aug, 2012"},"support_info":{"url":"","email":""},"background":"http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/730\/page_bg_generated_v6b.jpg?t=1467065027"}}}
Comments