2017-05-01 8 views
0

私はこれを使用します。オブジェクト(JSONから)がnullですか?

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をオブジェクトに変換するために同じコードを使用し、常に動作します。

答えて

0

不正なクラスに逆シリアル化しています。あなたのシリアル化コードはShotClass[]

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

の代わりに、あなたのJSON応答が(それが起動し、[ & ]で終わるので)配列であり、ショットのコレクションが含まれているためですRootobjectDribbbleShotsList

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

を使用するように変更します{ "id": 3467119, ... }。したがって、ShotClass []を使用して、ルートオブジェクトでラップする代わりに逆シリアル化する必要があります。

[ 
    { 
    "id": 3467119, 
    "title": "Swipe & Delete interaction", 
    ... 
    }, ... 
] 
関連する問題