skip to Main Content

I have my class defined as:

public class BusinessStatusHistory
{
    [JsonProperty("statusDate")]
    public string StatusDate { get; set; }

    [JsonProperty("addInfo")]
    public object AddInfo { get; set; }
}

object AddInfo can be a string or an anonymous object.

In my controller, I retrieve a list of BusinessStatusHistory and I can see the value for addInfo pop up:

enter image description here

When I return the response in this line: return Ok(result);

the value for addInfo is empty if it is an object and it shows if it is a string.

This is a sample response:

enter image description here

2

Answers


  1. public class BusinessStatusHistory
    {
        [JsonProperty("statusDate")]
        public string StatusDate { get; set; }
    
        [JsonProperty("addInfo")]
        public AddInfo AddInfo { get; set; }
    }
    
    public class AddInfo
    {
        [JsonProperty("status")]
        public List<object> Status { get; set; } // You can change List<object> with your data type
        
        [JsonProperty("trnRcptId")]
        public List<object> TrnRcptId { get; set; } // You can change List<object> with your data type
    
    }
    
    Login or Signup to reply.
  2. JObject doesn’t get recognized. You could try use ExpandoObject to parse the "AddInfo" to be recognized, Try following:

            public IActionResult Get()
            {
                var jsonstring = "{"status":"JHA Completed","trnRcptld":"JXFAKERJPT"}";
                var jobject = JObject.Parse(jsonstring);
                dynamic addinfo = new ExpandoObject();
    
                foreach (var item in jobject)
                {
                    var ln = item.Key.ToString();
    
                    (addinfo as IDictionary<string, object>)[ln] = item.Value.ToString();
                }
    
    
                var result = new BusinessStatusHistory { StatusDate = "2024-2-22", AddInfo = addinfo };
                return Ok(result);
            }
    
    

    Test result
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search