首页 \ 问答 \ 使用JSON.NET将平面JSON对象反序列化为复杂对象(Deserialising a flat JSON object into a complex object using JSON.NET)

使用JSON.NET将平面JSON对象反序列化为复杂对象(Deserialising a flat JSON object into a complex object using JSON.NET)

我正在从一个我无法控制的外部API中检索一些JSON,并且需要将其反序列化为一个对象,其中一些字段嵌套到主对象的属性中,因此直接反序列化将不起作用。

我发现的最接近的问题是:

Json .Net将平面对象序列化为复杂对象(在序列化/反序列化时更改对象结构)

我不能真正将它应用于我的问题,因为我对使用JSON.NET相当新,并且仍然试图理解它是如何工作的。

一个小样本,用于演示API中的json:

{
  FirstName: "John",
  LastName: "Smith",
  PassportNo: "xxxxx",
  NiNo: "xxxxx",
}

我想要反序列化的类:

internal sealed class Person
{
    [JsonProperty(PropertyName = "FirstName")]
    public string FirstName { get; set; }

    [JsonProperty(PropertyName = "LastName")]
    public string LastName { get; set; }

    public PrivateData PrivateData { get; set; }
}

internal sealed class PrivateData
{
    [JsonProperty(PropertyName = "PassportNo")]
    public string PassportNo { get; set; }

    [JsonProperty(PropertyName = "NiNo")]
    public string NationalInsuranceNumber { get; set; }
}

我不知道如何实现自定义合同解析器/ JSON转换器以获得所需的结果,因此任何指导都将受到赞赏。


I am retrieving some JSON from an external API that I have no control over and need to deserialise that into an object where some of the fields are nested into a property of the main object so a straight deserialise won't work.

The closest question I've found to my issue is:

Json .Net serialize flat object into a complex object (change objects structure on serialization/deserialization)

I couldn't really apply this to my problem though as i'm fairly new to using JSON.NET and still trying to understand how it works.

A small sample to demonstrate of the json back from the API:

{
  FirstName: "John",
  LastName: "Smith",
  PassportNo: "xxxxx",
  NiNo: "xxxxx",
}

The class(es) I want to deserialise into:

internal sealed class Person
{
    [JsonProperty(PropertyName = "FirstName")]
    public string FirstName { get; set; }

    [JsonProperty(PropertyName = "LastName")]
    public string LastName { get; set; }

    public PrivateData PrivateData { get; set; }
}

internal sealed class PrivateData
{
    [JsonProperty(PropertyName = "PassportNo")]
    public string PassportNo { get; set; }

    [JsonProperty(PropertyName = "NiNo")]
    public string NationalInsuranceNumber { get; set; }
}

I wasn't sure how to go about implementing a custom contract resolver / JSON converter to attain the required results so any guidance would be appreciated.


原文:https://stackoverflow.com/questions/40126886
更新时间:2023-06-08 12:06

最满意答案

我知道你发布这个已经有一段时间,但你一定要试试新的Facebook PHP SDK(可以在这里找到)。 使用新的Graph API,Facebook Connect的实现变得非常非常简单。


I know it has been awhile since you posted this but you should definitely try out the new Facebook PHP SDK (can be found here). With the new Graph API, the implementation of Facebook Connect has become very, very simple.

相关问答

更多
  • 可以推送到设备,但取决于您的设备。 解决方案是websockets,请参阅以下链接以进一步阅读: http://www.websocket.org/ http://code.google.com/p/phpwebsocket/ 另一个解决方案是longpolling,它很容易在php中实现: http : //en.wikipedia.org/wiki/Push_technology#Long_polling 在服务器端非常简单地实现longpolling: $ts = time(); while(true ...
  • 为了回答我自己的问题,我发现创建一个上下文并通过每个请求传递会话,我看到很多答案说cookie管理是自动的,但对我来说唯一的解决方案是通过整个上下文 BasicHttpContext context = new BasicHttpContext(); HttpGet get = new HttpGet("http://www.facebook.com/login.php"); HttpResponse response= HttpClientBuilder.create().bui ...
  • 以下是对旧的require_login函数的重写。 它完全重复旧功能。 function facebook_require_login($required_permissions = '') { global $facebook; // NOTE GLOBAL FACEBOOK OBJECT, MUST ALREADY BE INSTANTIATED $user = $facebook->get_loggedin_user(); $has_permissions = true; if ( ...
  • 您可以使用PHP SDK或Javascript SDK或No SDK或任意组合 ! 它很灵活,你喜欢编码的方式。 但是,由于您使用JS SDK进行登录,如果您希望您只使用JS SDK来实现应用程序的所有功能! 有没有办法只使用我不知道的JavaScript SDK? 你究竟是什么意思? 就像使用JS SDK登录使用FB.login() ,您可以调用其他API( 图形API ),如 - FB.api("/me") FB.api("/me/feed") FB.api("/me/accounts")等。等 你可以 ...
  • 您可以使用User Service插件在Openfire服务器上创建用户,但这不安全,因为客户端需要插件密钥才能执行此操作。 您的应用最好使用单独的身份验证服务器; 它的一个功能是通过这个插件在Openfire服务器上创建用户。 You can use the User Service plugin to create users on the Openfire server, but this won't be secure as the client requires the plugin secret ...
  • 你有没有看过KFacebook,Facebook的KDE客户端? KdePlayground和KFacebook 他们有一个Facebook API库和一个Plasma Applet库。 可能使用jQuery更容易。 但我认为,Qt4可以完成这项工作。 Have you seen KFacebook, the KDE client for Facebook? KdePlayground and KFacebook They have a library for Facebook API and a Plas ...
  • 您可以使用以下命令直接从Cookie中提取没有API调用的用户ID: $facebook->get_loggedin_user(); 请注意,这是在主$facebook对象上, 而不是它包含的api_client对象。 You can extract the user id without a API call, directly from the cookies, by using: $facebook->get_loggedin_user(); Note that this is on the ma ...
  • 我知道你发布这个已经有一段时间,但你一定要试试新的Facebook PHP SDK(可以在这里找到)。 使用新的Graph API,Facebook Connect的实现变得非常非常简单。 I know it has been awhile since you posted this but you should definitely try out the new Facebook PHP SDK (can be found here). With the new Graph API, the imple ...
  • 你看过Facebook Developer Toolkit了吗? http://facebooktoolkit.codeplex.com/ 如果您想从Silverlight中访问Facebook API,那么您应该从这里开始。 该项目利用上面的Facebook工具包: http://www.silverlight.net/content/samples/apps/facebookclient/sfcquickinstall.aspx 据我所知,它的来源尚未公布。 你可能会做类似的事情,但开源它。 Have ...
  • 如果您考虑TCP或UDP数据包标头中提供的内容,则不会包含太多身份信息,只包括IP地址。 如果您想知道客户的身份,您需要让他们发送某种唯一标识符(例如@madara评论的用户名和密码)。 如果它们来自相同的IP,这意味着它们使用相同的路由器,在这种情况下,其目的是掩盖路由器后面的设备。 要检测谁断开连接,首先需要确定谁连接。 每个连接都有自己的套接字,即使它们来自同一个IP地址。 在psuedo php中: // Store all active sockets in an array $online_us ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)