public Class Employee{
public string Name { get; set; }
[Column(TypeName = "jsonb")]
public List<Section> Sections { get; set; }
}
public Class Sections{
public string Booking { get; set; }
[Column(TypeName = "jsonb")]
public List<Interest> Interests { get; set; }
}
public Class Interest{
public string Title { get; set; }
public List<Meta> Meta { get; set; }
public List<WithAlt> Images { get; set; }
}
public Class Meta{
public string Type { get; set; }
public string Content { get; set; }
}
public Class WithAlt{
public string content { get; set; }
public string Alt { get; set; }
}
我從 Employee 表中獲取資料
員工在獲取我得到的資料 部分 Column
The JSON value could not be converted to System.String. Path: $[1].Interests[1].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 10073.
錯誤在
public Task<Employee> CheckEmployee(string name){
// error throw Line
var query= await catalogDbContext.Employee
.Where(i.Name === name)
.FirstOrDefault();
}
不是針對所有值,而是針對某些值List<Section> 或
List<Interest>或List<Meta>或List<WithAlt>具有空值
當我手動將值添加到部分列波紋管時
{
"Booking": "",
"Interests":[
{
"Title":"",
"Meta":[
{
"Type" : " ",
"Content" : " "
}
],
"Images" : [
{
"content" : " ",
"alt" : " "
}
]
}
],
}
它不會拋出錯誤
有什么方法可以使用代碼優先方法為上述欄位定義默認值
當我初始化 Sections 屬性時
public List<Section> Sections { get; set; }={};
它顯示以下錯誤
Can only use array initializer expressions to assign to array types. Try using a new expression instead.
并且
public List<Section> Sections { get; set; }= new List<Section> Sections();
和
public List<Meta> Meta { get; set; }= = new List<Meta>();
和
public List<WithAlt> Images { get; set; }= new List<WithAlt>();
扔 Error "The JSON value could not be converted to System.String. Path: $[1].Interests[1].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 10073."
uj5u.com熱心網友回復:
我只是對你的 json 進行反序列化,一切正常,我找不到任何錯誤
public static void Main()
{
var json = "{\"Booking\":\"\",\"Interests\":[{\"Title\":\"\",\"Meta\":[{\"Type\":\" \",\"Content\":\" \"}],\"Images\":[{\"content\":\" \",\"alt\":\" \"}]}]}";
var jd = JsonConvert.DeserializeObject<Data>(json);
}
班級
public class Data
{
public string Booking { get; set; }
public List<Interest> Interests { get; set; }
}
public class Interest
{
public string Title { get; set; }
public List<Meta> Meta { get; set; }
public List<Image> Images { get; set; }
}
public class Meta
{
public string Type { get; set; }
public string Content { get; set; }
}
public class Image
{
public string content { get; set; }
public string alt { get; set; }
}
uj5u.com熱心網友回復:
只能使用陣列初始值設定項運算式來分配給陣列型別。嘗試改用新運算式。
您可以將 json 資料轉換為Section型別而不是List<Section>型別。
var json = "{\"Booking\":\"\",\"Interests\":[{\"Title\":\"\",\"Meta\":[{\"Type\":\" \",\"Content\":\" \"}],\"Images\":[{\"content\":\" \",\"alt\":\" \"}]}]}";
var s = JsonConvert.DeserializeObject<Section>(json);
//If you want to set Employee.Sections with json data,try this
Employee e = new Employee { Sections = new List<Section> { s } };
模型(將類名更改Sections為Section,Interests為Interest):
public class Employee
{
public string Name { get; set; }
[Column(TypeName = "jsonb")]
public List<Section> Sections { get; set; }
}
public class Section
{
public string Booking { get; set; }
[Column(TypeName = "jsonb")]
public List<Interest> Interests { get; set; }
}
public class Interest
{
public string Title { get; set; }
public List<Meta> Meta { get; set; }
public List<WithAlt> Images { get; set; }
}
public class Meta
{
public string Type { get; set; }
public string Content { get; set; }
}
public class WithAlt
{
public string content { get; set; }
public string Alt { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321748.html
標籤:PostgreSQL 实体框架 asp.net核心 c#-4.0 实体框架-6
上一篇:LINQ查詢多個組和最新記錄的計數-OracleDB
下一篇:物體型別“X”需要定義一個主鍵
