Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法从Apple反序列化JSON';s iTunes流派ID服务_C#_Json_Windows Phone 8_Windows 8_Json.net - Fatal编程技术网

C# 无法从Apple反序列化JSON';s iTunes流派ID服务

C# 无法从Apple反序列化JSON';s iTunes流派ID服务,c#,json,windows-phone-8,windows-8,json.net,C#,Json,Windows Phone 8,Windows 8,Json.net,苹果iTunes流派ID服务返回的JSON与我过去遇到的其他JSON不同。正因为如此,我在构建反序列化所需的类时遇到了困难 下面是一个JSON示例(为了清晰起见,我将其删节;可以找到完整的JSON): 以下是我接收数据的课程: public class PodcastGenreResult { public PodcastGenreInfo GenreInfo { get; set; } } public class PodcastGenreInfo { [JsonProper

苹果iTunes流派ID服务返回的JSON与我过去遇到的其他JSON不同。正因为如此,我在构建反序列化所需的类时遇到了困难

下面是一个JSON示例(为了清晰起见,我将其删节;可以找到完整的JSON):

以下是我接收数据的课程:

public class PodcastGenreResult
{
    public PodcastGenreInfo GenreInfo { get; set; }
}

public class PodcastGenreInfo
{
    [JsonProperty("name")]
    public string GenreName { get; set; }
}
下面是反序列化调用:

PodcastGenreResult result = JsonConvert.DeserializeObject<PodcastGenreResult>(sResults);
PodcastGenreResult结果=JsonConvert.DeserializeObject(sResults);
其中,
sResults
是JSON字符串。我已确认此字符串包含正确的数据


在此之后,不会抛出错误,但
result.GenreInfo
始终为空。我也尝试过其他方法,但这是我认为最有效的方法。大多数情况下,我使用
JsonProperty
属性来指定我想要的对象,但我在这里不能指定,因为根名称会根据我请求的ID而改变。如何定义类以反序列化数据?

在我看来,问题在于json设计不当,使用的是类型id而不是属性名。json应该如下所示:

{"genre":{"name":"Podcasts","id":"26", ...
    "subgenres":{"genre": {"name":"Arts","id":"1301","url":
而不是:

{"26":{"name":"Podcasts","id":"26", ...
    "subgenres":{"1301":{"name":"Arts","id":"1301","url":
我会将json加载到动态对象中:

dynamic genres = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<dynamic>(json);
dynamic genres=new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(json);

如果需要,可以将动态对象转换为强类型对象。

这实际上相当常见。要处理键是动态的对象,需要将其反序列化到
字典中,其中
T
是您的项目类。因此,在你的情况下:

Dictionary<string, PodcastGenreInfo> genres = 
    JsonConvert.DeserializeObject<Dictionary<string, PodcastGenreInfo>>(sResults);
下面是一个工作演示:

class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        {
            string url = "http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=26";
            string json = client.DownloadString(url);
            var genres = JsonConvert.DeserializeObject<Dictionary<string, PodcastGenreInfo>>(json);
            Dump(new PodcastGenreInfo { GenreName = "Genres", Subgenres = genres }, "");
        }
    }

    private static void Dump(PodcastGenreInfo genre, string indent)
    {
        Console.WriteLine(indent + genre.GenreName);
        if (genre.Subgenres != null)
            foreach (var kvp in genre.Subgenres)
                Dump(kvp.Value, indent + "  ");
    }

    public class PodcastGenreInfo
    {
        [JsonProperty("name")]
        public string GenreName { get; set; }

        [JsonProperty("subgenres")]
        public Dictionary<string, PodcastGenreInfo> Subgenres { get; set; }
    }
}

放一些Json,这样我们就可以得到一个完整的画面。这非常有效。这也是有道理的,我从来没有使用过这条路线。谢谢,没问题;很高兴我能帮上忙。@brian rogers的回答对我有用,但我可以看出动态类型在这种情况下是多么有用。谢谢你的回复。
public class PodcastGenreInfo
{
    [JsonProperty("name")]
    public string GenreName { get; set; }

    [JsonProperty("subgenres")]
    public Dictionary<string, PodcastGenreInfo> Subgenres { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        using (WebClient client = new WebClient())
        {
            string url = "http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=26";
            string json = client.DownloadString(url);
            var genres = JsonConvert.DeserializeObject<Dictionary<string, PodcastGenreInfo>>(json);
            Dump(new PodcastGenreInfo { GenreName = "Genres", Subgenres = genres }, "");
        }
    }

    private static void Dump(PodcastGenreInfo genre, string indent)
    {
        Console.WriteLine(indent + genre.GenreName);
        if (genre.Subgenres != null)
            foreach (var kvp in genre.Subgenres)
                Dump(kvp.Value, indent + "  ");
    }

    public class PodcastGenreInfo
    {
        [JsonProperty("name")]
        public string GenreName { get; set; }

        [JsonProperty("subgenres")]
        public Dictionary<string, PodcastGenreInfo> Subgenres { get; set; }
    }
}
Genres
  Podcasts
    Arts
      Design
      Fashion & Beauty
      Food
      Literature
      Performing Arts
      Visual Arts
    Business
      Business News
      Careers
      Investing
      Management & Marketing
      Shopping
    Comedy
    Education
      Educational Technology
      Higher Education
      K-12
      Language Courses
      Training
    Games & Hobbies
      Automotive
      Aviation
      Hobbies
      Other Games
      Video Games
    Government & Organizations
      Local
      National
      Non-Profit
      Regional
    Health
      Alternative Health
      Fitness & Nutrition
      Self-Help
      Sexuality
    Kids & Family
    Music
    News & Politics
    Religion & Spirituality
      Buddhism
      Christianity
      Hinduism
      Islam
      Judaism
      Other
      Spirituality
    Science & Medicine
      Medicine
      Natural Sciences
      Social Sciences
    Society & Culture
      History
      Personal Journals
      Philosophy
      Places & Travel
    Sports & Recreation
      Amateur
      College & High School
      Outdoor
      Professional
    TV & Film
    Technology
      Gadgets
      Podcasting
      Software How-To
      Tech News