'How to access Json Object which has space in its name in MVC Controller Post method

{
  "Info": "",
  "ContactId": "6850R1644495860425",
  "Retailer Name": "Grow Retailer Shop One",
  "Contact Person": "Retailer One",
  "Mobile number": "8000000001",
  "Google Address": "Bait ul Kashif, Plot no 21, H no 13-6-823/A/9, Mehdi Colony, Langar Houz, Hyderabad, Telangana 50000",
  "SAP Address": "",
  "State*": "TELANGANA",
  "District*": "HYDERABAD",
  "Mandal": "Golconda",
  "Village": "Golconda",
  "RT": "",
  "Latitude": "17.3759033",
  "Longitude": "78.4266764",
  "Preferred Distributor": "",
  "PreferredSource1": "",
  "Territory": "HYDERABAD",
  "CreatedDate": "11/02/2022 05:26:28 AM",
  "CreatedBy": "GoIndigo FO",
  "Print": ""
}

this is my Json data which is coming from Ajax Post request to mvc controller which is having spaces in Properties i used [JsonProperty("Retailer Name")] also while deserializing but data coming null.



Solution 1:[1]

try to use a Newtonsoft.Json serializer since you are using [JsonProperty] attribute. This attribute doesn't work for System.Text.Json.

Data data = JsonConvert.DeserializeObject<Data>(json);

and class

    public partial class Data
    {
      
        public string Info { get; set; }

       
        public string ContactId { get; set; }

        [JsonProperty("Retailer Name")]
        public string RetailerName { get; set; }

        [JsonProperty("Contact Person")]
        public string ContactPerson { get; set; }

        [JsonProperty("Mobile number")]
        public string MobileNumber { get; set; }

        [JsonProperty("Google Address")]
        public string GoogleAddress { get; set; }

        [JsonProperty("SAP Address")]
        public string SapAddress { get; set; }

        public string State { get; set; }

        public string District { get; set; }

        public string Mandal { get; set; }

      
        public string Village { get; set; }

      
        public string Rt { get; set; }

        public string Latitude { get; set; }

        public string Longitude { get; set; }

        [JsonProperty("Preferred Distributor")]
        public string PreferredDistributor { get; set; }

        public string PreferredSource1 { get; set; }

        public string Territory { get; set; }

        public string CreatedDate { get; set; }

       
        public string CreatedBy { get; set; }

        public string Print { get; set; }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1