【问题标题】:Error "The JSON value could not be converted to System.String. Path: $[1].Interests[1].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 10073."错误“无法将 JSON 值转换为 System.String。路径:$[1].Interests[1].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 10073。”
【发布时间】:2021-12-03 16:22:36
【问题描述】:
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 表中获取数据

员工在获取数据时 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&lt;Section&gt;List&lt;Interest&gt;List&lt;Meta&gt;List&lt;WithAlt&gt; 具有空值

当我手动将值添加到下面的部分列时

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

【问题讨论】:

  • 请问可以发原始的json吗?
  • {“预订”:“”,“兴趣”:[{“标题”:“”,“元”:[{“类型”:“”,“内容”:“”}] , "图片" : [ { "content" : " ", "alt" : " " } ] } ], }

标签: postgresql entity-framework asp.net-core c#-4.0 entity-framework-6


【解决方案1】:

只能使用数组初始化表达式来分配数组类型。请尝试使用新的表达式。

您可以将json数据转换为Section类型而不是List&lt;Section&gt;类型。

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 更改为 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; }

    }

【讨论】:

    【解决方案2】:

    我刚刚对你的 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; }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 2021-10-11
      • 2022-12-14
      • 2019-04-05
      • 2014-03-14
      • 2017-11-24
      • 2020-02-25
      • 2013-08-15
      • 2020-10-05
      相关资源
      最近更新 更多