2

I am trying to create a model that would match the JSON.NET deserealization of this JSON structure: First item...

    {
    "190374": {
    "vid": 190374,
    "canonical-vid": 190374,
    "portal-id": 62515,
    "is-contact": true,
    "profile-token": "AO_T-mN1n0Mbol1q9X9UeCtRwUE1G2GFUt0VVxCzpxUF1LJ8L3i75x9NmhIiS0K9UQkx19bShhlUwlIujY4pSXAFPEfDG-k9n8BkbftPw6Y5oM3eU5Dc_Mm-5YNJTXiWyeVSQJAN_-Xo",
    "profile-url": "https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mN1n0Mbol1q9X9UeCtRwUE1G2GFUt0VVxCzpxUF1LJ8L3i75x9NmhIiS0K9UQkx19bShhlUwlIujY4pSXAFPEfDG-k9n8BkbftPw6Y5oM3eU5Dc_Mm-5YNJTXiWyeVSQJAN_-Xo/",
    "properties": {
    "phone": {
    "value": "null"
    },
    "hs_social_linkedin_clicks": {
    "value": "0"
    },
    "hs_social_num_broadcast_clicks": {
    "value": "0"
    },
    "hs_social_facebook_clicks": {
    "value": "0"
    },
    "state": {
    "value": "MA"
    },
    "createdate": {
    "value": "1380897795295"
    },
    "hs_analytics_revenue": {
    "value": "0.0"
    },
    "lastname": {
    "value": "Mott"
    },
    "company": {
    "value": "HubSpot"
    }
    },
    "form-submissions": [],
    "identity-profiles": []
    },
     "form-submissions": [],
    "identity-profiles": []
    },
**next similar entry**

Then I try to bind with List of this:

public class HubSpotEmailRqst
{
    public int vid { get; set; }
    public int canonical-vid { get; set; }
    public int portal-id { get; set; }
    public bool is-contact { get; set; }
    public String profile-token { get; set; }
    public String profile-url { get; set; }
    public Dictionary<string, string> properties { get; set; }
    public Dictionary<string,string> form-submissions { get; set; }
    public Dictionary<string,string> identity-profiles { get; set; }
}

I am not sure this will bind either, but I cannot get past the fact we cannot have hyphen in the field names, how can I get around this?

RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53

2 Answers2

7

There're two ways to use properties with hyphens:

  1. Name properties according to C# rules, but decorate them with [JsonProperty("property-name")] attribute.

  2. Use custom contract resolver which modifies property names. For example, if all properties in JSON are named consistently, you can use regex to change PascalCase C# property names to lower-case JSON property names. See CamelCasePropertyNamesContractResolver from Json.NET for example implementation.

Athari
  • 33,702
  • 16
  • 105
  • 146
1

If I'm understanding the question correctly, you are upset that you can't use a hyphen in a field name in a C# program? Why not use CamelCase to identify the fields? Instead of is-contact use 'isContact'. Instead of canonical-vid. use canonicalVid. Sure, the names may look different, but to the programmer the meaning should be clear.

Kevin
  • 4,798
  • 19
  • 73
  • 120
  • This has crossed my mind, however I do not have the luxury of changing the field names, because the JSON is generated by a third party. – RealityDysfunction Feb 04 '14 at 21:29
  • 2
    Then you might have to process it before hand, and map the field names to the expected name. – rae1 Feb 04 '14 at 21:31