hi ,
i have web api in asp.net c# should return json format like this :
{
"evidence": {
"file":"JVBERi0xLjQNCjEgMCBvYmoNCjw8DQovQ3JlYXRvciAoT3JhY2xlMTFnUjEgQVMgUmVwb3J0cyBTZXJ2aWNlcykNCi9DcmVhdG",
fileType:"pdf"}
}
}
but in some cases i need to return the same object with only two brackets this the requirment like this:
{
"evidence": {}
}
model class structure :
public class evidenceObj
{
public string file { get; set; }
public string fileType { get; set; }
}
can any one help me i tried too much but couldnt find result
thank you

Amit MohantyRank #112 · 20K reputationPosted Nov 20, 2023, 1:15 AM EST
Check the below example, this may help you.
public IHttpActionResult GetApiResponse(bool condition) { try { var evidence = new EvidenceObj { File = "JVBERi0xLjQNCjEgMCBvYmoNCjw8DQovQ3JlYXRvciAoT3JhY2xlMTFnUjEgQVMgUmVwb3J0cyBTZXJ2aWNlcykNCi9DcmVhdG", FileType = "pdf" }; ApiResponse response = null; if (condition) response = new ApiResponse { Evidence = evidence }; else response = new ApiResponse { Evidence = new EvidenceObj() }; return new RawJsonActionResult(JsonConvert.SerializeObject(response)); } catch (Exception e) { logger.Error(e.Message); throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("Error : " + e.Message), ReasonPhrase = "Critical Exception" }); } }Model class:
public class EvidenceObj { [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string File { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public string FileType { get; set; } } public class ApiResponse { public EvidenceObj Evidence { get; set; } }