C# 如何在.NETCore2.2中将StringifyJSON对象转换为模型类?

C# 如何在.NETCore2.2中将StringifyJSON对象转换为模型类?,c#,json,.net-core,C#,Json,.net Core,我正在接收json格式的string对象。现在我在.netcore public class AgentSessions { // public BsonObjectId _id {get; set;} public string[] socketID {get; set;} public string agent_id {get; set;} public string nsp {get; set;} public string createdDat

我正在接收
json
格式的
string
对象。现在我在
.netcore

public class AgentSessions 
{
    // public BsonObjectId _id {get; set;}
    public string[] socketID {get; set;}
    public string agent_id {get; set;}
    public string nsp {get; set;}

    public string createdDate {get; set;}
    public string nickname {get; set;}
    public string email {get; set;}
    public ExpandoObject rooms {get; set;}

    public int chatCount {get; set;}
    public string type{get; set;}
    public string[] location {get; set;}
    public int visitorCount {get; set;}
            public string role {get; set;}
    public Boolean acceptingChats {get; set;}
    public string state {get; set;}
    public ExpandoObject[]  idlePeriod {get; set;}

    public string image {get; set;}
    public ExpandoObject locationCount{get; set;}

    public ExpandoObject callingState{get; set;}

    public ExpandoObject permissions{get; set;}
    public dynamic groups {get; set;}

    public dynamic teams {get; set;}

    public dynamic  isOwner {get; set;}

    public Boolean updated {get; set;}

    public int concurrentChatLimit{get; set;}
    public ExpandoObject conversationState{get; set;}

    public  string id{get; set;}

    public Boolean inactive{get; set;}

    public string lastTouchedTime{get; set;}
    public DateTime? expiry{get; set;}

    public DateTime endingDate{get; set;}
}
现在我必须将
json
对象(字符串)转换为我定义的
类型。我该怎么做

   stringify data object that i am receiving 

"session":{"_id":"5e2acd287ce5f349008348f5","socketID":[],"agent_id":"5c41ade033aa87307ca9831f","nsp":"/localhost.com","createdDate":"2020-01-24T10:55:36.293Z","nickname":"Nickname Changing","email":"saad_cuteguy11@hotmail.com","rooms":{},"chatCount":0,"type":"Agents","location":[],"visitorCount":0,"role":"admin","acceptingChats":true,"state":"ACTIVE","idlePeriod":[],"image":"https://elasticbeanstalk-us-west-1-021594099427.s3.amazonaws.com/userUploads%2Fhrm.sbtjapan.com%2Fsaadisheikh9705%40sbtjapan.com%2Fpp%2F1546264191938.jpg","locationCount":{},"callingState":{"socketid":"","state":false,"agent":""},"permissions":{"tickets":{"enabled":true,"canMerge":true,"canCreate":true,"canViewLog":true,"canGroup":true,"allowCC":true,"allowBCC":true,"canView":"all","canAssignAgent":true,"canAssignGroup":true,"canAddNote":true,"canAddTag":true,"canAddTask":true,"canChangeState":true,"canExport":true,"canSetPriority":true,"canSnooze":true,"canAddGroupAdmins":true},"chats":{"enabled":true,"allowEmoji":true,"allowAttachments":true,"allowVoicenotes":true,"allowCalling":true,"allowAddAsFaq":true,"allowChatTransfer":true,"allowTypingStatus":true,"canView":"all","canChat":true},"agents":{"enabled":true,"canCreate":true,"canEdit":true,"canChat":true,"canCall":true,"canViewStats":true,"canChangeOwnPassword":true,"canChangeOthersPassword":true},"settings":{"enabled":true,"automatedResponses":{"enabled":true},"rolesAndPermissions":{"enabled":true,"canView":["admin","supervisor","agent","TEst"],"canAddRole":true,"canModifyOwn":true,"canModifyOther":true,"canDeleteRole":true},"formDesigner":{"enabled":true},"ticketManagement":{"enabled":true},"chatTimeouts":{"enabled":true},"callSettings":{"enabled":true},"contactSettings":{"enabled":true},"chatWindowSettings":{"enabled":true},"chatAssistant":{"enabled":true},"webhooks":{"enabled":true},"integerations":{"enabled":true},"knowledgeBase":{"enabled":true},"widgetMarketing":{"enabled":true}},"dashboard":{"enabled":true},"visitors":{"enabled":true},"analytics":{"enabled":true,"visitors":true,"canView":"all","chats":true,"agents":true,"tickets":true},"crm":{"enabled":true},"chatbot":{"enabled":true},"installation":{"enabled":true},"updatedOn":"2020-01-07T11:50:43.690Z"},"groups":["Congo1"],"teams":[],"isOwner":false,"updated":true,"concurrentChatLimit":2,"conversationState":false,"id":"5e2acd287ce5f349008348f5","inactive":false,"lastTouchedTime":"2020-01-24T10:55:37.344Z","endingDate":"2020-01-24T10:55:56.980Z"}

另一个问题是,如果我的
json
对象(字符串格式)中没有任何值,会发生什么

使用NewtonsoftJson nuget

它看起来像:

var session = JsonConvert.DeserializeObject<AgentSessions>(stringThatUReceived);

由于您使用的是.netcore,因此可以使用microsoft提供的新System.Text.Json

var session = JsonSerializer.Deserialize<AgentSessions>(stringYouRecieved);

var session=JsonSerializer。反序列化是Javascript将对象转换为JSON的方式。它与JSON内容本身无关。您发布的只是一个JSON字符串,需要使用例如JSON.NET或System.Text.JSONI来解析。我在.NETCore中工作。我正在接收stringify对象,我想将该stringify对象解析到我定义的类中。所以请给我一个答案,我该如何做,以及我该如何在我的模式类(c#)中声明可为null的值?没有所谓的
stringify对象
。您发布的只是一个JSON字符串。您可以使用JSON.NETAs对其进行反序列化以获取可空值,您已经定义了这些值。我如何在模型类中声明可空字段?因为有些领域optional@FahadHassan在问题文本中添加您的问题。此外,您已经定义了可空字段。此请求的标题表明它适用于.NET Core 2.2,在这种情况下,新的system.text.json命名空间不可用(它是在.NET Core 3.0中添加的)。您是对的,如果NET Core 3.0不是一个选项,您必须坚持使用Newtonsoft
var session = JsonSerializer.Deserialize<AgentSessions>(stringYouRecieved);