How to select some Json field in a response with out saveing whole part?

fahime abouhamze 126 Reputation points
2021-12-05T07:33:21.077+00:00

I get a response witch is like bellow:

  {
  "status": 1,
  "result": {
  "id": "28",
  "userName": "Sara",
  "city": "177",
  "cityName": "sari",
  "address": "sadr ave...",
  "documents": [
  {
    "documentTypeId": "3",
    "fileName": "a.jpg",
    "fileType": "image/jpeg",
    "content": "aaaa"
  },
  {
    "documentTypeId": "2",
    "fileName": "b.jpg",
    "fileType": "image/jpeg",
    "content": "aaa"
      }
    ]
  }

}

I want to have response Be like above but with out "content" so
I define the output properties like bellow :

        public class Response
    {
        public int status { get; set; }
        public List<result> result { get; set; }
     }

public class result
    {
        public int id { get; set; }
        public string userName { get; set; }
        public int city { get; set; }
        public string cityName { get; set; }
        public string address { get; set; }
        public List<documents> documents { get; set; }
    }


    public class documents
    {
        public int documentTypeId { get; set; }
        public string fileName { get; set; }
        public string fileType { get; set; }
    }

I do not want to get "content" so I eliminate it In document class
but my response is just like bellow :

  {"status":1,"result":[]}

what can I do to get response like above with out content :
my code to get response is like bellow :

bodyRequest.Content = new StringContent(JsonConvert.SerializeObject(new { caseId = caseId }), 
Encoding.UTF8, "application/json");
var responseApi = Client.PostAsync(baseAddress, bodyRequest.Content, 
 new System.Threading.CancellationToken(false)).Result;
 if (responseApi.StatusCode == HttpStatusCode.OK)
     {
         ResponseText = responseApi.Content.ReadAsStringAsync().Result;
         var res = jss.Deserialize<ApiResponse>(ResponseText);
     }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,403 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,696 Reputation points
    2021-12-05T12:37:40.15+00:00

    The main issue is the object model design does not match the JSON data format.

        public class Response  
        {  
            public int status { get; set; }  
            public Result result { get; set; }  
        }  
      
        public class Result  
        {  
            public string id { get; set; }  
            public string userName { get; set; }  
            public string city { get; set; }  
            public string cityName { get; set; }  
            public string address { get; set; }  
            public Document[] documents { get; set; }  
        }  
      
        public class Document  
        {  
            public string documentTypeId { get; set; }  
            public string fileName { get; set; }  
            public string fileType { get; set; }  
            [JsonIgnore]  
            public string content { get; set; }  
        }  
    

    Use the [JsonIgnore] attribute]1 to set properties you do not want serialized. Keep in mind, the service you are calling returns the content property.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful