坚持反序列化JSON Web响应

时间:2017-07-02 13:23:05

标签: c# json

我在反序列化以下JSON字符串时遇到了一些问题。在我的所有其他回复中,它的效果非常好但不适用于此。我尝试了很多不同的东西,我检查了Stream类大约100次。我也使用了json2csharp中生成的类,但我的所有反序列化数据都是NULL。

获取回复的代码:

    public Stream getStream(string userID)
    {
        var request = GetRequest("streams/{channelID}", Method.GET);
        request.AddUrlSegment("channelID", userID);
        var response = restClient.Execute<Stream>(request);
        return response.Data;
    }

response.Content向我展示了类似的例子:

{
"stream":
    {
    "_id":25649270464,
    "game":"test",
    "broadcast_platform":"other",
    "community_id":"xxxxxxxxx",
    "community_ids":["xxxxxx"],
    "viewers":2421,
    "video_height":1080,
    "average_fps":61.7876424715,
    "delay":0,
    "created_at":"2017-07-02T10:01:28Z",
    "is_playlist":false,
    "stream_type":"live",
    "preview":
        {
        "small":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-80x45.jpg",
        "medium":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-320x180.jpg",
        "large":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-640x360.jpg",
        "template":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-{width}x{height}.jpg"
        },
    "channel":
        {
        "mature":false,
        "status":"Test"
        ,"broadcaster_language":"de",
        "display_name":"LOSTAIMING",
        "game":"test",
        "language":"en",
        "_id":44281267,
        "name":"lostaiming",
        "created_at":"2013-06-02T16:42:19.329009Z",
        "updated_at":"2017-07-02T13:05:11.555285Z",
        "partner":true,
        "logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/lostaiming-profile_image-e9d7ea0893748d6a-300x300.png",
        "video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/cc34c6b909a435ae-channel_offline_image-1920x1080.png",
        "profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/c187e8871c0f6a2b-profile_banner-480.png",
        "profile_banner_background_color":"",
        "url":"https://www.twitch.tv/lostaiming",
        "views":2292173,
        "followers":55672,
        "broadcaster_type":"",
        "description":"Blubb"
        }
    }
}

我的Stream类看起来像这样:

    class Stream
{
    [JsonProperty("stream")]
    public SubStream stream { get; set; }
}
class SubStream
{      
    [JsonProperty("_id")]
    public string ID { get; set; }
    [JsonProperty("game")]
    public string Game { get; set; }
    [JsonProperty("broadcast_platform")]
    public string BroadcastPlatform { get; set; }       
    [JsonProperty("community_id")]
    public string CommunityID { get; set; }

    [JsonProperty("community_ids")]
    public List<object> CommunityIDS { get; set; }
    [JsonProperty("viewers")]
    public long Viewers { get; set; }
    [JsonProperty("video_height")]
    public long VideoHeigt { get; set; }
    [JsonProperty("average_fps")]
    public double AverageFps { get; set; }
    [JsonProperty("delay")]
    public long Delay { get; set; }
    [JsonProperty("createt_at")]
    public DateTime CreatetAt { get; set; }
    [JsonProperty("is_playlist")]
    public bool IsPlaylist { get; set; }
    [JsonProperty("stream_type")]
    public string StreamType { get; set; }
    [JsonProperty("preview")]
    public StreamPreview Preview { get; set; }
    [JsonProperty("channel")]
    public Channel channel { get; set; }


}
class StreamPreview
{
    [JsonProperty("small")]
    public string SmallPreview { get; set; }
    [JsonProperty("medium")]
    public string MediumPreview { get; set; }
    [JsonProperty("large")]
    public string LargPreview { get; set; }
    [JsonProperty("template")]
    public string TemplatePreview { get; set; }
}


public class Channel
{
    [JsonProperty("mature")]
    public bool Mature { get; set; }
    [JsonProperty("status")]
    public string Status { get; set; }
    [JsonProperty("broadcaster_language")]
    public string BroadcasterLanguage { get; set; }
    [JsonProperty("display_name")]
    public string DisplayName { get; set; }
    [JsonProperty("game")]
    public string Game { get; set; }
    [JsonProperty("language")]
    public string Language { get; set; }
    [JsonProperty("_id")]
    public string ID { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("created_at")]
    public DateTime CreatedAt { get; set; }
    [JsonProperty("updated_at")]
    public DateTime UpdatedAt { get; set; }
    [JsonProperty("partner")]
    public bool Partner { get; set; }             
    [JsonProperty("logo")]
    public string Logo { get; set; }
    [JsonProperty("video_banner")]
    public string VideoBanner { get; set; }
    [JsonProperty("profile_banner")]
    public string ProfileBanner { get; set; }
    [JsonProperty("profile_banner_background_color")]
    public string ProfileBannerBackgroundColor { get; set; }
    [JsonProperty("url")]
    public string Url { get; set; }
    [JsonProperty("views")]
    public long Views { get; set; }
    [JsonProperty("followers")]
    public long Followers { get; set; }
    [JsonProperty("broadcaster_type")]
    public string BroadcastType { get; set; }

    [JsonProperty("stream_key")]
    public string StreamKey { get; set; }
    [JsonProperty("email")]
    public string Email { get; set; }
}

2 个答案:

答案 0 :(得分:0)

1)使用JSON Utils(https://jsonutils.com/)生成C#类(对于属性选择JsonProperty)

2)Newtonsoft.Json几乎是使用.NET中JSON格式的标准最佳库,你可以从nuget获得它。

3)代码应该看起来像这样,反序列化应该没有任何问题:

    class Program
    {
        private static void Main(string[] args)
        {
            var myObj = JsonConvert.DeserializeObject<JsonDef>(json);

            Console.Read();
        }
    }

    public class Preview
    {

        [JsonProperty("small")]
        public string small { get; set; }

        [JsonProperty("medium")]
        public string medium { get; set; }

        [JsonProperty("large")]
        public string large { get; set; }

        [JsonProperty("template")]
        public string template { get; set; }
    }

    public class Channel
    {

        [JsonProperty("mature")]
        public bool mature { get; set; }

        [JsonProperty("status")]
        public string status { get; set; }

        [JsonProperty("broadcaster_language")]
        public string broadcaster_language { get; set; }

        [JsonProperty("display_name")]
        public string display_name { get; set; }

        [JsonProperty("game")]
        public string game { get; set; }

        [JsonProperty("language")]
        public string language { get; set; }

        [JsonProperty("_id")]
        public int _id { get; set; }

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

        [JsonProperty("created_at")]
        public DateTime created_at { get; set; }

        [JsonProperty("updated_at")]
        public DateTime updated_at { get; set; }

        [JsonProperty("partner")]
        public bool partner { get; set; }

        [JsonProperty("logo")]
        public string logo { get; set; }

        [JsonProperty("video_banner")]
        public string video_banner { get; set; }

        [JsonProperty("profile_banner")]
        public string profile_banner { get; set; }

        [JsonProperty("profile_banner_background_color")]
        public string profile_banner_background_color { get; set; }

        [JsonProperty("url")]
        public string url { get; set; }

        [JsonProperty("views")]
        public int views { get; set; }

        [JsonProperty("followers")]
        public int followers { get; set; }

        [JsonProperty("broadcaster_type")]
        public string broadcaster_type { get; set; }

        [JsonProperty("description")]
        public string description { get; set; }
    }

    public class Stream
    {

        [JsonProperty("_id")]
        public long _id { get; set; }

        [JsonProperty("game")]
        public string game { get; set; }

        [JsonProperty("broadcast_platform")]
        public string broadcast_platform { get; set; }

        [JsonProperty("community_id")]
        public string community_id { get; set; }

        [JsonProperty("community_ids")]
        public IList<string> community_ids { get; set; }

        [JsonProperty("viewers")]
        public int viewers { get; set; }

        [JsonProperty("video_height")]
        public int video_height { get; set; }

        [JsonProperty("average_fps")]
        public double average_fps { get; set; }

        [JsonProperty("delay")]
        public int delay { get; set; }

        [JsonProperty("created_at")]
        public DateTime created_at { get; set; }

        [JsonProperty("is_playlist")]
        public bool is_playlist { get; set; }

        [JsonProperty("stream_type")]
        public string stream_type { get; set; }

        [JsonProperty("preview")]
        public Preview preview { get; set; }

        [JsonProperty("channel")]
        public Channel channel { get; set; }
    }

    public class JsonDef
    {

        [JsonProperty("stream")]
        public Stream stream { get; set; }
    }

答案 1 :(得分:0)

这是因为JSON的DateTime格式。它不是一个格式良好的.Net DateTime。所以你可以把它作为一个字符串,然后按你想要的方式转换它或将其格式改为可读的.Net格式。