I am using System.Text.Json.Serialization
with JsonPropertyName("obj.property")
in a .NET Core 3.1 based web api project to deserialize JSON input to a C# POCO object. The input contains few attributes with dots, i.e.,
"id": "B1346",
"businessType": "RENTAL",
"company.city": "LUND",
"company.zipcode": "99999",
"company.email": "[email protected]",
"user.email[0]": "[email protected]",
"user.name[0]": "Tom",
"user.email[1]": "[email protected]",
"user.name[1]": "Harry",
I have created following POCO object:
public class Form
{
public string Id { get; set;}
public string BusinessType { get; set;}
[JsonPropertyName("company.city")]
public string CompanyCity { get; set;}
[JsonPropertyName("company.zipcode")]
public string CompanyZipcode { get; set;}
[JsonPropertyName("company.email")]
public string CompanyEmail { get; set;}
[JsonPropertyName("user.email")]
public List<string> UserEmail { get; set;}
[JsonPropertyName("user.name")]
public List<string> UserName { get; set;}
}
So far it has worked for simple attributes with dot, e.g., company.city
, but not for array elements, e.g., users.name[0]
public async Task PostApplication(Form form)
{ ... }
Quick watch (during debug) shows the successful deserialization of all properties for the parameter "form" except arrays:
CodePudding user response:
assuming that you have only [0] and [1] try this classes
public class Root
{
public string id { get; set; }
public string businessType { get; set; }
[JsonProperty("company.city")]
public string CompanyCity { get; set; }
[JsonProperty("company.zipcode")]
public string CompanyZipcode { get; set; }
[JsonProperty("company.email")]
public string CompanyEmail { get; set; }
[JsonProperty("user.email[0]")]
public string UserFirsEmail { get; set; }
[JsonProperty("user.name[0]")]
public string UserFirstName { get; set; }
[JsonProperty("user.email[1]")]
public string UserSecondEmail { get; set; }
[JsonProperty("user.name[1]")]
public string UserSecondName { get; set; }
}
if you have arrays with more than 2 elements you need much more complicated code. But I can't see anything else in your post.
var jD = JsonConvert.DeserializeObject<Root>(json);
var data = new Data {
Id=jD.id,
BusinessType=jD.businessType,
Company = new Company { City = jD.CompanyCity, Email = jD.CompanyEmail },
User = new User {Name=jD.UserName0, Email=jD.UserEmail0, SecondName=jD.UserName1, SecondEmail=jD.UserEmail1}
}
classes
public class Data
{
public string Id { get; set; }
public string BusinessType { get; set; }
public Company Company {get;set;}
public User User {get;set;}
}
public class Company
{
public string City { get; set; }
public string Zipcode { get; set; }
public string Email { get; set; }
}
public class User
{
public string Email { get; set; }
public string Name { get; set; }
public string SecondEmail { get; set; }
public string SecondName { get; set; }
}
CodePudding user response:
The items in the list need to be JSON list format, it should look like this to be deserialized as a string list :
{
"id": "B1346",
"businessType": "RENTAL",
"company.city": "LUND",
"company.zipcode": "99999",
"company.email": "[email protected]",
"user.email": ["[email protected]", "[email protected]"]
"user.name": ["Tom","Harry]
}