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

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

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; }

}

我从员工那里获取数据table

Employee while fetching the data Sections 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" : " "
      }
    ]
  }
],

  }

不会抛出错误

有没有办法使用代码优先方法为上述字段定义默认值

当我初始化部分时 属性 喜欢

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."

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

Can only use array initializer expressions to assign to array types. Try using a new expression instead.

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

型号(将 class 名称 Sections 更改为 SectionInterests 更改为 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; }

    }