Reputation: 7532
I have some JSON I am trying to deserialize (https://api.zype.com/consumers/?api_key=qfcR4WWqBws1ezeUHNxTTQl8ucuuP9pW1rMssmQBFUE-AbpPhKp_cY0RnfpY57y5):
I have a Consumer class and 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
I'm trying to deserialize the JSON:
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)
I get this error:
Error converting value "cus_AHtHxKXCwe6MPZ" to type 'System.Collections.Generic.List`1[System.String]'. Path 'response[3].stripe_id', line 1, position 2043.
I don't see anywhere the JSON is malformed and the above value is in fact a string. What am I doing wrong and how do I fix this?
Upvotes: 1
Views: 116
Reputation: 3668
This structure works for me
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; }
}
Following is the code to access the elements
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();
}
Update I am using c# code because your tag is C#.
Upvotes: 1