Home > Software engineering >  read JSON value in ASP.NET Core web api
read JSON value in ASP.NET Core web api

Time:09-22

What is the best approach to get value from:

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

CodePudding user response:

try this using 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;

or you can install Newtonsoft.Json nuget package and use this code

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

classes

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; }
    }

  • Related