如何在ASP.NET中进行对象的序列化?
我正在尝试将API发送给我的对象,但是当这样做时,我无法提取值。
我作为JSON的对象看起来像这样:
{
"Obj":
{
"id":19,
"name":"test",
"email":"[email protected]",
"password":"0439434dae91c10c3bc073af1e76addf8f57a30ce0a7de0438b3aaad34b85200d41d01078f2ee786b3130b4ed4e39e3e26090da5d9f87420454dfdd182761cce",
"city":"Texas",
"age":37,
"date":"2022-05-09T00:00:00",
"mp":0,
"du":0,
"active":false,
"userid":0,
"user":""
},
"message":null,
"error":false,
"information":
{
"menssages":"Ok, Results",
"error":false,
"success":true,
"userId":0,
"user":"",
"register":0,
"pages":0
}
}
我的代码:
result = GetWebRequest("api/ClientId/" + id);
object rest = new JavaScriptSerializer().DeserializeObject(result.ToString());
Dictionary<string, object> keys = (Dictionary<string, object>)((object)rest);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 json.net for both性能以及功能。
如果您使用的是ASP.NET core (从您的问题/标签中不太清楚),您也可以使用内置 system.text.json :
这两种方法都更干净。当您使用类而不是
字典
时。并不是说字典
在任何意义上都是错误的,这只是它的最佳用例,尤其是因为您正在处理嵌套实例。尽管通常,键入对数据的属性的访问几乎总是首选的解决方案。I'd suggest using JSON.Net for both performance as well as features.
If you're using ASP.NET Core (not quite clear from your question/tags), you can also use the built-in System.Text.Json:
Both approaches are much cleaner as you're working with classes instead of a
Dictionary
. Not that aDictionary
is wrong in any sense, this is just not an optimal use case for it, in particular because you're dealing with nested instances. Though in general, typed access to the properties of your data is almost always a preferred solution.