对象(来自JSON)是null?

时间:2017-05-01 10:15:36

标签: c# json object uwp

当我使用它时:

RootobjectDribbbleShotsList ObjShots = await DribbbleShotsListProxy.GetShots(App.accesstoken, "debuts", "20", "day", "likes");

ObjShots是空的。

这是DribbbleShotsListProxy类:

using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace MyProject
{
    public class DribbbleShotsListProxy
    {
        public async static Task<RootobjectDribbbleShotsList> GetShots(string token, string type, string limit, string timeframe, string sort)
        {

            var http = new HttpClient();
            var url = "https://api.dribbble.com/v1/shots?access_token=" + token + "&list=" + type + "&timeframe=" + "&sort=" + sort + "&per_page=" + limit;
            var response = await http.GetAsync(url);
            var result = await response.Content.ReadAsStringAsync();
            var serializer = new DataContractJsonSerializer(typeof(RootobjectDribbbleShotsList));
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
            var data = (RootobjectDribbbleShotsList)serializer.ReadObject(ms);

            Debug.WriteLine(url);

            return data;
        }
    }


    [DataContract]
    public class RootobjectDribbbleShotsList
    {
        [DataMember]
        public ShotClass[] Shot { get; set; }
    }

    [DataContract]
    public class ShotClass
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string title { get; set; }                                              
        [DataMember]
        public string description { get; set; }                                        
        [DataMember]
        public int width { get; set; }                                                 
        [DataMember]
        public int height { get; set; }                                                
        [DataMember]
        public Images images { get; set; }                                             
        [DataMember]
        public int views_count { get; set; }                                           
        [DataMember]
        public int likes_count { get; set; }                                           
        [DataMember]
        public int comments_count { get; set; }                                        
        [DataMember]
        public int attachments_count { get; set; }                                     
        [DataMember]
        public int rebounds_count { get; set; }                                        
        [DataMember]
        public int buckets_count { get; set; }                                         
        [DataMember]
        public string created_at { get; set; }                                         
        [DataMember]
        public string updated_at { get; set; }                                         
        [DataMember]
        public string html_url { get; set; }                                           
        [DataMember]
        public string attachments_url { get; set; }                                    
        [DataMember]
        public string buckets_url { get; set; }                                        
        [DataMember]
        public string comments_url { get; set; }                                       
        [DataMember]
        public string likes_url { get; set; }                                          
        [DataMember]
        public string projects_url { get; set; }                                       
        [DataMember]
        public string rebounds_url { get; set; }                                       
        [DataMember]
        public bool animated { get; set; }                                             
        [DataMember]
        public string[] tags { get; set; }                                             
        [DataMember]
        public User user { get; set; }                                                 
        [DataMember]
        public Team team { get; set; }                                                 
        [DataMember]
        public string rebound_source_url { get; set; }
    }

    [DataContract]
    public class Images
    {
        [DataMember]
        public string hidpi { get; set; }
        [DataMember]
        public string normal { get; set; }
        [DataMember]
        public string teaser { get; set; }
    }

    [DataContract]
    public class User
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string name { get; set; }                                                
        [DataMember]
        public string username { get; set; }                                            
        [DataMember]
        public string html_url { get; set; }                                            
        [DataMember]
        public string avatar_url { get; set; }                                          
        [DataMember]
        public string bio { get; set; }                                                 
        [DataMember]
        public string location { get; set; }                                            
        [DataMember]
        public Links links { get; set; }                                                
        [DataMember]
        public int buckets_count { get; set; }                                          
        [DataMember]
        public int comments_received_count { get; set; }                                
        [DataMember]
        public int followers_count { get; set; }                                        
        [DataMember]
        public int followings_count { get; set; }                                       
        [DataMember]
        public int likes_count { get; set; }                                            
        [DataMember]
        public int likes_received_count { get; set; }                                   
        [DataMember]
        public int projects_count { get; set; }                                         
        [DataMember]
        public int rebounds_received_count { get; set; }                                
        [DataMember]
        public int shots_count { get; set; }                                            
        [DataMember]
        public int teams_count { get; set; }                                            
        [DataMember]
        public bool can_upload_shot { get; set; }                                       
        [DataMember]
        public string type { get; set; }                                                
        [DataMember]
        public bool pro { get; set; }                                                   
        [DataMember]
        public string buckets_url { get; set; }                                         
        [DataMember]
        public string followers_url { get; set; }                                       
        [DataMember]
        public string following_url { get; set; }                                       
        [DataMember]
        public string likes_url { get; set; }                                           
        [DataMember]
        public string projects_url { get; set; }                                        
        [DataMember]
        public string shots_url { get; set; }                                           
        [DataMember]
        public string teams_url { get; set; }                                           
        [DataMember]
        public string created_at { get; set; }                                          
        [DataMember]
        public string updated_at { get; set; }                                          
        [DataMember]
        public int members_count { get; set; }
        [DataMember]
        public string members_url { get; set; }
        [DataMember]
        public string team_shots_url { get; set; }
    }

    [DataContract]
    public class Links
    {
        [DataMember]
        public string web { get; set; }
        [DataMember]
        public string twitter { get; set; }
    }

    [DataContract]
    public class Team
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string username { get; set; }
        [DataMember]
        public string html_url { get; set; }
        [DataMember]
        public string avatar_url { get; set; }
        [DataMember]
        public string bio { get; set; }
        [DataMember]
        public string location { get; set; }
        [DataMember]
        public Links1 links { get; set; }
        [DataMember]
        public int buckets_count { get; set; }
        [DataMember]
        public int comments_received_count { get; set; }
        [DataMember]
        public int followers_count { get; set; }
        [DataMember]
        public int followings_count { get; set; }
        [DataMember]
        public int likes_count { get; set; }
        [DataMember]
        public int likes_received_count { get; set; }
        [DataMember]
        public int projects_count { get; set; }
        [DataMember]
        public int rebounds_received_count { get; set; }
        [DataMember]
        public int shots_count { get; set; }
        [DataMember]
        public bool can_upload_shot { get; set; }
        [DataMember]
        public string type { get; set; }
        [DataMember]
        public bool pro { get; set; }
        [DataMember]
        public string buckets_url { get; set; }
        [DataMember]
        public string followers_url { get; set; }
        [DataMember]
        public string following_url { get; set; }
        [DataMember]
        public string likes_url { get; set; }
        [DataMember]
        public string projects_url { get; set; }
        [DataMember]
        public string shots_url { get; set; }
        [DataMember]
        public string created_at { get; set; }
        [DataMember]
        public string updated_at { get; set; }
        [DataMember]
        public int members_count { get; set; }
        [DataMember]
        public string members_url { get; set; }
        [DataMember]
        public string team_shots_url { get; set; }
    }

    [DataContract]
    public class Links1 {
        [DataMember]
        public string web { get; set; }
        [DataMember]
        public string twitter { get; set; }
    }

}

响应始终是有效的JSON(响应示例:https://pastebin.com/raw/dLFsc1jg

我不知道为什么这段代码不起作用。我总是使用相同的代码将JSON转换为对象,它始终有效。

1 个答案:

答案 0 :(得分:0)

您正在反序列化为不正确的类。更改序列化代码以使用ShotClass[]

var serializer = new DataContractJsonSerializer(typeof(ShotClass[]));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (ShotClass[])serializer.ReadObject(ms);

而不是RootobjectDribbbleShotsList

// Fails to deserialize
var serializer = new DataContractJsonSerializer(typeof(RootobjectDribbbleShotsList));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (RootobjectDribbbleShotsList)serializer.ReadObject(ms);

这是因为您的JSON响应是一个数组(因为它以[&amp; ]开头和结尾)并包含一组镜头{ "id": 3467119, ... }。因此,您需要使用ShotClass []来反序列化,而不是将其包装在根对象中。

[
  {
    "id": 3467119,
    "title": "Swipe & Delete interaction",
    ...
  }, ...
]