进行建模以匹配此JSON以进行反序列化,字段名称带有破折号 [英] Model to match this JSON for deserialization, field names with dashes

查看:148
本文介绍了进行建模以匹配此JSON以进行反序列化,字段名称带有破折号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个与该JSON结构的JSON.NET反现实化匹配的模型: 第一项...

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?

推荐答案

使用带连字符的属性有两种方法:

There're two ways to use properties with hyphens:

  1. 根据C#规则命名属性,但使用[JsonProperty("property-name")]属性装饰它们.

使用可修改属性名称的自定义合同解析器.例如,如果JSON中的所有属性均一致命名,则可以使用regex将PascalCase C#属性名称更改为小写JSON属性名称.有关示例实现,请参见Json.NET中的CamelCasePropertyNamesContractResolver.

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.

这篇关于进行建模以匹配此JSON以进行反序列化,字段名称带有破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆