我在一個基于.NET Core 3.1的web api專案中使用System.Text.Json.Serialization與JsonPropertyName("obj.property")來反序列化JSON輸入到一個C# POCO物件。輸入包含一些帶點的屬性,即:
"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",
我已經創建了以下的POCO物件:
public class Form
{
public string Id { get; set; }
public string BusinessType { get; set; }
[
到目前為止,它對帶點的簡單屬性有效,例如:company.city,但對陣列元素無效,例如:users.name[0]
public async 任務 PostApplication(Form form)。
{ ... }
快速觀察(在除錯期間)顯示,除了陣列之外,引數 "form "的所有屬性都被成功反序列化了:
uj5u.com熱心網友回復:
假設你只有[0]和[1],試試這個類
public class Root
{
public string id { get; set; }
public string businessType { get; set; }
[]
public string CompanyCity { get; set; }
[
如果你有超過2個元素的陣列,你需要更復雜的代碼。但我在你的帖子中看不到其他內容。
var jD = JsonConvert.DeserializeObject<Root> (json)。
var data = new Data {
Id=jD.id。
BusinessType=jD.businessType,
公司 = new Company { City = jD.CompanyCity, Email = jD.CompanyEmail },
User = new User {Name=jD.UserName0, Email=jD.UserEmail0, SecondName=jD.UserName1, SecondEmail=jD.UserEmail1}.
}
類
public class Data
{
public string Id { get; set; }
public string BusinessType { get; set; }
public 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; }
uj5u.com熱心網友回復:
串列中的專案需要是JSON串列格式,它應該像這樣被反序列化為一個字串串列 :
{
"id"。"B1346"。
"businessType": "RENTAL",
"公司.城市": "LUND",
"company.zipcode": "99999",
"company.email": "[email protected]",
"user.email": ["[email protected]"/span>, "[email protected]"/span>]
"user.name": ["Tom","Harry]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307047.html
標籤:
下一篇:<p>我試圖從我的javacode向我的nodeJS應用程式發送一個jsonArray,但我一直在nodejs端得到這個錯誤,我難以理解這是為什么?因為發送一個正常的JSONObject可
