如何在ASP.NET中进行对象的序列化?

发布于 2025-02-01 12:36:15 字数 1133 浏览 2 评论 0 原文

我正在尝试将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);

I am trying to deserialize an object that an API sends me, but when doing so I cannot extract the values.

My object as JSON looks like this:

{
   "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
    }
}

My code:

    result = GetWebRequest("api/ClientId/" + id);
    object rest = new JavaScriptSerializer().DeserializeObject(result.ToString());
    
    Dictionary<string, object> keys = (Dictionary<string, object>)((object)rest);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-02-08 12:36:15

我建议使用 json.net for both性能以及功能

var account = JsonConvert.DeserializeObject<Account>(jsonString);

如果您使用的是ASP.NET core (从您的问题/标签中不太清楚),您也可以使用内置 system.text.json

var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);

这两种方法都更干净。当您使用类而不是字典时。并不是说字典在任何意义上都是错误的,这只是它的最佳用例,尤其是因为您正在处理嵌套实例。尽管通常,键入对数据的属性的访问几乎总是首选的解决方案。

I'd suggest using JSON.Net for both performance as well as features.

var account = JsonConvert.DeserializeObject<Account>(jsonString);

If you're using ASP.NET Core (not quite clear from your question/tags), you can also use the built-in System.Text.Json:

var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString);

Both approaches are much cleaner as you're working with classes instead of a Dictionary. Not that a Dictionary 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文