2

I have below Json :-

{
    "EventMessageUId": "ef51b5a3-32b2-e611-baf9-fc3fdb446bd2",

    "Message": [{
        "StoryID": 1,
        "StoryDesc": "xyzzzz"
    }],
    "ProjectUId": "00100000-0000-0000-0000-000000000000",
    "ProjectId": 1,
    "CreatedByUser": "system",
    "CreatedByApp": "myWizard-Fortress",
    "CreatedOn": "2016-11-24T10:44:39.473"
}

I have made binding classes as :-

public class Requirements
    {
        public string EventMessageUId { get; set; }
        public int ProjectId { get; set; }
        public string CreatedByUser { get; set; }
        public string CreatedByApp { get; set; }        
        public string CreatedOn { get; set; }
        Message obj = new Message();
    }

    public class Message
    {
        public string StoryID { get; set; }
        public string StoryDesc { get; set; }
    }

I am mapping it as below :-

Requirements objRequirement = JsonObject.ToObject<Requirements>();

But when I see through HOver :-

enter image description here

As we can see in the Image :-

Message obj = new Message();

Obj is becoming Null

How can I map it in the correct way ?

Edit 1 :

I changed my class to :-

 public class Requirements
    {     
        public string EventMessageUId { get; set; }
        public int ProjectId { get; set; }
        public string CreatedByUser { get; set; }
        public string CreatedByApp { get; set; }        
        public string CreatedOn { get; set; }
        public Message Requirement { get; set; }        
    }

Same problem :- enter image description here

4 Answers 4

2

Just change your class so that there is a message property. In your class it is called obj. The message property should also be public so that it can be set.

public class Requirements
{
    public string EventMessageUId { get; set; }
    public int ProjectId { get; set; }
    public string CreatedByUser { get; set; }
    public string CreatedByApp { get; set; }        
    public string CreatedOn { get; set; }
    public Message message { get; set; }
}

public class Message
{
    public string StoryID { get; set; }
    public string StoryDesc { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

please see me Edit 1
Just change this line: public Message Requirement { get; set; } to public Message Message { get; set; }
The name in your JSON and C# should be the same: "Message"
@CSharper Rajshekar Reddy is correct your json object seems to have a list or array of messages. Meaning multiple Messages. try: public List<Message> Message {get; set;}
1

Modify your first class as follows:

public Message[] Message { get; set; } 

1 Comment

You are correct, looking at the JSON Message is an array: "Message": [{ "StoryID": 1, "StoryDesc": "xyzzzz" }], If you look at the [{}]
1

I see 3 possible issues :

  • In your Json you reffer the Message object by the name Message, in your class definition, its name is obj
  • In your Json, the Message object is set as an array
  • Your Message object is not settable

{
    "EventMessageUId": "ef51b5a3-32b2-e611-baf9-fc3fdb446bd2",

    "Message": {
        "StoryID": 1,
        "StoryDesc": "xyzzzz"
    },
    "ProjectUId": "00100000-0000-0000-0000-000000000000",
    "ProjectId": 1,
    "CreatedByUser": "system",
    "CreatedByApp": "myWizard-Fortress",
    "CreatedOn": "2016-11-24T10:44:39.473"
}

public class Requirements
{
    public string EventMessageUId { get; set; }
    public int ProjectId { get; set; }
    public string CreatedByUser { get; set; }
    public string CreatedByApp { get; set; }        
    public string CreatedOn { get; set; }

    private Message _Message = new Message();
    public Message Message  { get { return this._Message ; } set { this._Message = value; } }

}
public class Message
{
   public string StoryID { get; set; }
   public string StoryDesc { get; set; }
}

Comments

1

You need a property named Message which is an Collection (use List) in your Requirements class. Remove the Message obj = new Message();

Add this to your class

public List<Message> Message {get; set;} 

So your class definition must look like.

public class Requirements
{
    public string EventMessageUId { get; set; }
    public int ProjectId { get; set; }
    public string CreatedByUser { get; set; }
    public string CreatedByApp { get; set; }        
    public string CreatedOn { get; set; }
    public List<Message> Message {get; set;}  //add this
}

Comments

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.