使用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:
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
最满意答案
我知道你发布这个已经有一段时间,但你一定要试试新的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.
相关问答
更多-
PHP - echo做其他客户端(PHP - echo do other client)[2022-03-19]
可以推送到设备,但取决于您的设备。 解决方案是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")等。等 你可以 ...
-
使用Facebook等从Android客户端登录Openfire Server(Use Facebook etc. to Login to Openfire Server from Android Client)[2022-09-26]
您可以使用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 ... -
用于Facebook的Qt客户端(Qt client for Facebook)[2022-11-15]
你有没有看过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客户端(Facebook php client)[2023-05-02]
我知道你发布这个已经有一段时间,但你一定要试试新的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 ...