错误"The JSON value could not be converted to System.String. Path: $[1].Interests[1].Meta[9].Content |


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表中获取数据

Employee在获取数据时SectionsColumn我得到

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>为空值

当我手动添加值到sections列下面

{
"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>();

throwError "The JSON value could not be converted to System.String. Path: $[1].Interests[1].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 10073."

只能使用数组初始化表达式赋值给数组类型。试着用一个新的表达式来代替。

您可以将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 } };

Models(将类名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; }
}

我刚刚反序列化了你的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; }
}