json.net array richtig auslesen

  • C#
  • .NET (FX) 4.0

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von KingBurger.

    json.net array richtig auslesen

    Hallo, ich versuche mit Hilfe von json.net den Array der Steam Store API auszulesen.
    store.steampowered.com/api/appdetails?appids=730

    Allerdings bekomme ich immer folgende Fehlermeldung:

    Quellcode

    1. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ShopReader.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.


    Meine Klassen:
    Spoiler anzeigen

    C#-Quellcode

    1. public class PcRequirements
    2. {
    3. public string minimum { get; set; }
    4. }
    5. public class MacRequirements
    6. {
    7. public string minimum { get; set; }
    8. }
    9. public class LinuxRequirements
    10. {
    11. public string minimum { get; set; }
    12. }
    13. public class PriceOverview
    14. {
    15. public string currency { get; set; }
    16. public int initial { get; set; }
    17. public int final { get; set; }
    18. public int discount_percent { get; set; }
    19. }
    20. public class Sub
    21. {
    22. public int packageid { get; set; }
    23. public string percent_savings_text { get; set; }
    24. public int percent_savings { get; set; }
    25. public string option_text { get; set; }
    26. public string option_description { get; set; }
    27. public string can_get_free_license { get; set; }
    28. public bool is_free_license { get; set; }
    29. public int price_in_cents_with_discount { get; set; }
    30. }
    31. public class PackageGroup
    32. {
    33. public string name { get; set; }
    34. public string title { get; set; }
    35. public string description { get; set; }
    36. public string selection_text { get; set; }
    37. public string save_text { get; set; }
    38. public int display_type { get; set; }
    39. public string is_recurring_subscription { get; set; }
    40. public List<Sub> subs { get; set; }
    41. }
    42. public class Platforms
    43. {
    44. public bool windows { get; set; }
    45. public bool mac { get; set; }
    46. public bool linux { get; set; }
    47. }
    48. public class Metacritic
    49. {
    50. public int score { get; set; }
    51. public string url { get; set; }
    52. }
    53. public class Category
    54. {
    55. public int id { get; set; }
    56. public string description { get; set; }
    57. }
    58. public class Genre
    59. {
    60. public string id { get; set; }
    61. public string description { get; set; }
    62. }
    63. public class Screenshot
    64. {
    65. public int id { get; set; }
    66. public string path_thumbnail { get; set; }
    67. public string path_full { get; set; }
    68. }
    69. public class Webm
    70. {
    71. public string __invalid_name__480 { get; set; }
    72. public string max { get; set; }
    73. }
    74. public class Movie
    75. {
    76. public int id { get; set; }
    77. public string name { get; set; }
    78. public string thumbnail { get; set; }
    79. public Webm webm { get; set; }
    80. public bool highlight { get; set; }
    81. }
    82. public class Recommendations
    83. {
    84. public int total { get; set; }
    85. }
    86. public class Highlighted
    87. {
    88. public string name { get; set; }
    89. public string path { get; set; }
    90. }
    91. public class Achievements
    92. {
    93. public int total { get; set; }
    94. public List<Highlighted> highlighted { get; set; }
    95. }
    96. public class ReleaseDate
    97. {
    98. public bool coming_soon { get; set; }
    99. public string date { get; set; }
    100. }
    101. public class SupportInfo
    102. {
    103. public string url { get; set; }
    104. public string email { get; set; }
    105. }
    106. public class Data
    107. {
    108. public string type { get; set; }
    109. public string name { get; set; }
    110. public int steam_appid { get; set; }
    111. public int required_age { get; set; }
    112. public bool is_free { get; set; }
    113. public string controller_support { get; set; }
    114. public string detailed_description { get; set; }
    115. public string about_the_game { get; set; }
    116. public string supported_languages { get; set; }
    117. public string header_image { get; set; }
    118. public string website { get; set; }
    119. public PcRequirements pc_requirements { get; set; }
    120. public MacRequirements mac_requirements { get; set; }
    121. public LinuxRequirements linux_requirements { get; set; }
    122. public List<string> developers { get; set; }
    123. public List<string> publishers { get; set; }
    124. public PriceOverview price_overview { get; set; }
    125. public List<int> packages { get; set; }
    126. public List<PackageGroup> package_groups { get; set; }
    127. public Platforms platforms { get; set; }
    128. public Metacritic metacritic { get; set; }
    129. public List<Category> categories { get; set; }
    130. public List<Genre> genres { get; set; }
    131. public List<Screenshot> screenshots { get; set; }
    132. public List<Movie> movies { get; set; }
    133. public Recommendations recommendations { get; set; }
    134. public Achievements achievements { get; set; }
    135. public ReleaseDate release_date { get; set; }
    136. public SupportInfo support_info { get; set; }
    137. public string background { get; set; }
    138. }
    139. public class steam
    140. {
    141. public bool success { get; set; }
    142. public Data data { get; set; }
    143. }
    144. public class RootObject
    145. {
    146. public steam steam { get; set; }
    147. }


    Der Main Teil:

    C#-Quellcode

    1. static void Main(string[] args)
    2. {
    3. string json = new WebClient().DownloadString("http://store.steampowered.com/api/appdetails?appids=730");
    4. var jobject = JsonConvert.DeserializeObject<List<RootObject>>(json);
    5. foreach(var datas in jobject)
    6. {
    7. Console.WriteLine(datas.steam.data.name);
    8. }
    9. Console.ReadKey();
    10. }
    Noch dazu ist folgende Zeile falsch.

    C#-Quellcode

    1. var jobject = JsonConvert.DeserializeObject<List<RootObject>>(json);


    Hier nimmste nur das eine RootObject, anstatt List<RootObject>;

    C#-Quellcode

    1. var jobject = JsonConvert.DeserializeObject<RootObject>(json);
    2. Console.WriteLine(jobject.steam.data.name);
    And i think to myself... what a wonderfuL World!