Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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# 将JSON反序列化为自定义类的列表_C#_Asp.net_Json_Json.net - Fatal编程技术网

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

C# 将JSON反序列化为自定义类的列表,c#,asp.net,json,json.net,C#,Asp.net,Json,Json.net,我有一些JSON正在尝试反序列化(): 我有一个消费类和ZypeConsumerList: Public Class Consumer <JsonProperty("email")> Public Property Email As String <JsonProperty("stripe_id")> Public Property StripeId As List(Of String) <JsonProperty("_id"

我有一些JSON正在尝试反序列化():

我有一个消费类和ZypeConsumerList:

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
我得到这个错误:

 Error converting value "cus_AHtHxKXCwe6MPZ" to type 'System.Collections.Generic.List`1[System.String]'. Path 'response[3].stripe_id', line 1, position 2043.

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

这个结构适合我

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; }
}
公共类消费者
{
公共字符串_id{get;set;}
公共字符串amazon_user_id{get;set;}
公共字符串生日{get;set;}
公共字符串braintree_id{get;set;}
在{get;set;}处创建的公共字符串
公共字符串电子邮件{get;set;}
公共字符串名称{get;set;}
公共字符串pass_count{get;set;}
公共字符串密码\u令牌{get;set;}
公共字符串播放列表\u计数{get;set;}
公共字符串memory_标记{get;set;}
公共字符串rss_标记{get;set;}
公共字符串sex{get;set;}
公共字符串site_id{get;set;}
公共字符串stripe_id{get;set;}
公共字符串订阅\u计数{get;set;}
公共字符串项{get;set;}
公共字符串事务计数{get;set;}
公共字符串更新{get;set;}
公共字符串视频_计数{get;set;}
公共列表链接的_设备{get;set;}
}
公共类分页
{
当前公共字符串{get;set;}
前一个公共字符串{get;set;}
公共字符串next{get;set;}
每个页面的公共字符串{get;set;}
公共字符串页{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();
    }
publicstaticvoidmain(字符串[]args)
{
使用(var stream=newstreamreader(“sample.json”))
{
var rootObject=JsonConvert.DeserializeObject(stream.ReadToEnd());
int指数=0;
foreach(rootObject.response中的var响应)
{
控制台.WriteLine(响应.stripe\u id);
}
WriteLine(rootObject.pagination.current);
WriteLine(rootObject.pagination.next);
Console.WriteLine(rootObject.pagination.pages);
Console.WriteLine(rootObject.pagination.per_page);
WriteLine(rootObject.pagination.previous);
}
Console.Read();
}

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

stipe\u id不是字符串吗?你在你的类中将它定义为一个列表(字符串)。当你的代码是VB时,为什么要将它标记为C?我不知道你是如何创建这些类的,但我通常做的是根据json结果生成类,以避免像你这样的错误。
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();
    }