Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can you deserialize JSON data in C# (using DataContractJsonSerializer) without knowing all property names?
    primarykey
    data
    text
    <p>I have been using the DataContractJsonSerializer to convert data returned from the HubSpot API into strongly-typed objects, but I'm having some trouble with the user profile object.</p> <p>In this example, I am able to get the Id and IsContact properties, but can't figure out how to get the list of properties since I don't know in advance what those can be. I would like to make Properties a Dictionary but I'm not sure how to do this. I don't care about the versions for each property, just the value.</p> <p>This is a simplified example of the data that is returned by the API:</p> <pre><code>{ "vid": 72361, "is-contact": true, "properties": { "city": { "value": "Burlington", "versions": [ { "value": "Burlington", "source-type": "SALESFORCE", "source-id": "continuous", "source-label": null, "timestamp": 1384319976006, "selected": false } ] }, "country": { "value": "US", "versions": [ { "value": "US", "source-type": "SALESFORCE", "source-id": "continuous", "source-label": null, "timestamp": 1384319976006, "selected": false } ] }, "company": { "value": "Bridgeline Digital", "versions": [ { "value": "Bridgeline Digital", "source-type": "SALESFORCE", "source-id": "continuous", "source-label": null, "timestamp": 1384319976006, "selected": false } ] } } } </code></pre> <p>This is the object I am trying to deserialize to:</p> <pre><code>[DataContract] public class HubSpotUserProfile { [DataMember(Name = "vid")] public int Id { get; set; } [DataMember(Name = "is-contact")] public bool IsContact { get; set; } [DataMember(Name = "redirect")] public string RedirectUrl { get; set; } [DataMember(Name = "properties")] public Dictionary&lt;string, HubSpotUserProfileProperty&gt; Properties { get; set; } } [DataContract] public class HubSpotUserProfileProperty { [DataMember(Name = "value")] public string Value { get; set; } } </code></pre> <p>I call this method to perform the deserialization:</p> <pre><code> public static T Post&lt;T&gt;(string url, string postData) where T : class { string json = Post(url, postData); if (!String.IsNullOrWhiteSpace(json)) { using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(stream); } } return null; } </code></pre> <p>When I do this, no error is thrown, but Properties always has a Count of 0. Any idea on how I can accomplish this goal?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload