将JSON反序列化为自定义类的列表

时间:2017-04-20 16:26:32

标签: c# asp.net json json.net

我有一些JSON我试图反序列化(https://api.zype.com/consumers/?api_key=qfcR4WWqBws1ezeUHNxTTQl8ucuuP9pW1rMssmQBFUE-AbpPhKp_cY0RnfpY57y5):

我有一个Consumer类和ZypeConsumerList:

var a = 1;
function b() {
  a = 10; // here var a is declared and defined locally because it uses a var keyword. 
  return;

  function foo() {}
}
b();
console.log(a); // logs a = 1

我正在尝试反序列化JSON:

Public Class Consumer
    <JsonProperty("email")>
    Public Property Email As String
    <JsonProperty("stripe_id")>
    Public Property StripeId As List(Of String)
    <JsonProperty("_id")>
    Public Property Id As String
    <JsonProperty("_keywords")>
    Public Property Keywords As List(Of String)
    <JsonProperty("created_at")>
    Public Property CreatedOn As Nullable(Of Date)
    <JsonProperty("updated_at")>
    Public Property UpdatedOn As Nullable(Of Date)
    <JsonProperty("deleted_at")>
    Public Property DeletedOn As Nullable(Of Date)
    <JsonProperty("site_id")>
    Public Property SiteId As String
    <JsonProperty("subscription_count")>
    Public Property SubscriptionCount As Int16
End Class

Public Class ZypeConsumerList
    <JsonProperty("response")>
    Public Property Consumers As List(Of DanceNetwork.Zype.Consumer)
    <JsonProperty("pagination")>
    Public Property Pagination As DanceNetwork.Zype.Pagination
End Class

我收到此错误:

        Dim zResponse As ZypeResponse = myCall.Execute(ZypeRestEndpoint.Consumers)
        Dim responseBlock As ZypeConsumerList
        Dim mySerializerSettings As JsonSerializerSettings = New JsonSerializerSettings()
        mySerializerSettings.NullValueHandling = NullValueHandling.Ignore
        responseBlock = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ZypeConsumerList)(zResponse.data, mySerializerSettings)

我没有看到JSON格式错误的任何地方,上面的值实际上是一个字符串。我做错了什么,如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

这个结构适合我

public class Consumer
{
    public string _id { get; set; }
    public string amazon_user_id { get; set; }
    public string birthday { get; set; }
    public string braintree_id { get; set; }
    public string created_at { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public string pass_count { get; set; }
    public string password_token { get; set; }
    public string playlist_count { get; set; }
    public string remember_token { get; set; }
    public string rss_token { get; set; }
    public string sex { get; set; }
    public string site_id { get; set; }
    public string stripe_id { get; set; }
    public string subscription_count { get; set; }
    public string terms { get; set; }
    public string transaction_count { get; set; }
    public string updates { get; set; }
    public string video_count { get; set; }
    public List<string> linked_devices { get; set; }
}

public class Pagination
{
    public string current { get; set; }
    public string previous { get; set; }
    public string next { get; set; }
    public string per_page { get; set; }
    public string pages { get; set; }
}

以下是访问元素的代码

public static void Main(string[] args)
    {
        using (var stream = new StreamReader("sample.json"))
        {

            var rootObject = JsonConvert.DeserializeObject<Root>(stream.ReadToEnd());
            int index = 0;
            foreach (var responses in rootObject.response)
            {

                Console.WriteLine(responses.stripe_id);
            }
            Console.WriteLine(rootObject.pagination.current);
            Console.WriteLine(rootObject.pagination.next);
            Console.WriteLine(rootObject.pagination.pages);
            Console.WriteLine(rootObject.pagination.per_page);
            Console.WriteLine(rootObject.pagination.previous);


        }

        Console.Read();
    }

更新我正在使用c#代码,因为您的代码是C#。