0

I am receiving json object in string format. Now i have my model class like this in .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;}
}

Now i have to convert that json object(string) into my defined class type. How can i do that?

   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":"[email protected]","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"}

And another question is that what happened if any value will not be there in my json object(string format) ?

7
  • stringify is how Javascript converts an object to JSON. It has nothing to do with the JSON content itself. What you posted is simply a JSON string that needs to be parsed using eg Json.NET or System.Text.JSON Commented Jan 24, 2020 at 13:38
  • I am working in .net core. I am receiving stringify object and i want to parse that stringify object into my defined class. So please give me answer that how can i do that Commented Jan 24, 2020 at 13:39
  • And also how can i declare nullable values in my mode class(c#) ? Commented Jan 24, 2020 at 13:40
  • There's no such thing as a stringify object. What you posted is just a JSON string. You can deserialize it with JSON.NET Commented Jan 24, 2020 at 13:43
  • As for nullable values, you already defined them. Commented Jan 24, 2020 at 13:44

2 Answers 2

1

Use NewtonsoftJson nuget.

It will looks like:

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

Second: it will be set to default(T) in ur object (not sure)

As comment(I need 50 rep to write comments, lol): Simply write something like this for value-type variables

public int? chatCount { get; set; }
Sign up to request clarification or add additional context in comments.

2 Comments

And how can i declare nullable fields in my model class? because some fields are optional
@FahadHassan add your questions in the question's text. Besides, you already defined nullable fields.
0

since you are on .netcore you can use the new System.Text.Json from microsoft.

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

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to

2 Comments

The title of this request suggests that it is for .NET Core 2.2, in which case the new system.text.json namespace isn't available (it was added in .NET Core 3.0).
you are right, if net core 3.0 is not an option, you have to stick Newtonsoft

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.