【问题标题】:read JSON value in ASP.NET Core web api在 ASP.NET Core Web api 中读取 JSON 值
【发布时间】:2021-11-12 23:17:48
【问题描述】:

获取价值的最佳方法是:

{"profileObj":{"Name":"xxx","FirstName":"xxx","FastName":"xxx","Email":"xxx"}}

【问题讨论】:

    标签: c# json asp.net-core .net-core asp.net-web-api


    【解决方案1】:

    使用 System.Text.Json 试试这个

    //your json
    
    var json= "{\"profileObj\":{\"Name\":\"xxx\",\"FirstName\":\"xxx\",\"FastName\":\"xxx\",\"Email\":\"xxx\"}}";
    
    using System.Text.Json;
    .....
    
    var jD= JsonSerializer.Deserialize<Root>(json);
    
    var value =jD.profileObj;
    
    var name= value.Name;
    
    

    或者您可以安装 Newtonsoft.Json nuget 包并使用此代码

    using Newtonsoft.Json;
    .....
    var jD = JsonConvert.DeserializeObject<Root>(json);
    ......
    
    

    类

    public class ProfileObj
        {
            public string Name { get; set; }
            public string FirstName { get; set; }
            public string FastName { get; set; }
            public string Email { get; set; }
        }
    
        public class Root
        {
            public ProfileObj profileObj { get; set; }
        }
    
    

    【讨论】:

    • @FarhadZamani 谢谢。我更新了我的答案。净 text.json 应该足够了。这是一个非常简单的对象。
    • 是的,这就够了。但是,添加命名空间可以帮助用户更多
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2020-02-11
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多