system.text.json.jsonexception:JSON值无法转换为System.String。

发布于 01-22 09:47 字数 3811 浏览 2 评论 0原文

我正在使用PostgreSQL进行ASP.NET核心项目。

我写了一个查询以获取列表arrivalguide

// this is working fine
var x = await _catalogDbContext.Destinations.ToListAsync();  

// But I got error for this query
var y = await _catalogDbContext.ArrivalGuide.ToListAsync();  

我有一个错误:

失败:Microsoft.EntityFrameWorkCore.Query [10100]

在上下文类型'techne.travelhub.data.catalogdbcontext'的查询结果上迭代时发生了例外。

system.text.json.jsonexception:JSON值无法转换为System.String。路径:$ [9] .PointSofestests [3] .Meta [9] .CONTENT |亚麻布:0 | Bytepositioninline:103060。

system.invalidoperationException:无法将令牌类型的“ startObject”的值作为字符串。

在system.text.json.utf8jsonreader.getstring()
在System.text.json.json.jsonpropertyinfonotnullable`4.4.onread(readStack& state,utf8jsonreader& reader)
在System.Text.json.jsonserializer.ReadCore(JSonSerializerOptions选项,UTF8JSONREADER& reader,ReadStack& readstack)

---内部异常堆栈跟踪的结尾---
在System.text.json.throwhelper.rethrowwithpath(ReadStack&readStack,utf8jsonreader& reader,exception ex)
在System.Text.json.jsonserializer.ReadCore(JSonSerializerOptions选项,UTF8JSONREADER& reader,ReadStack& readstack)

在System.Text.json.jsonserializer.ReadCore(键入returnType,jsonserialializeoptions选项,utf8jsonreader& reader)
在system.text.json.jsonserializer.deserialize(字符串json,type returnType,jsonserialializeptions选项)
在system.text.json.jsonserializer.deserialize [tvalue](字符串JSON,JSONSERIALIALIAMEOPTIONS选项)
在npgsql.typehandlers.jsonhandler.read [t](npgsqlreadbuffer buf,int32 bytelen,boolean async,fieldDescription field fieldDescription)<

public class ArrivalGuide
{
     public List<DestinationSection> Sections { get; set; }
     // removed rest
}

public class DestinationSection
{
    public List<PointsOfInterest> PointsOfInterests { get; set; }
    // removed rest
}

public class PointsOfInterest
{
    public List<PointOfInterestMeta> Meta { get; set; }
}

public class PointOfInterestMeta
{
    public string Type { get; set; }
    public string Content { get; set; }
}

fieldDescription

builder.Property(i => i.Sections)
            .HasColumnType("jsonb");
    
builder.Ignore(i => i.Video);

fieldDescription

编辑:节-JSONB

[
  {
    "Iso": "eating",
    "PointsOfInterests": [
      {
        "Meta": [
          {
            "Type": "address",
            "Content": "1A Bailuzhou Dong Lu, Siming District, Xiamen"
          },
          {
            "Type": "phone",
            "Content": "+86 592 5330 888"
          },
          {
            "Type": "www",
            "Content": null
          },
          {
            "Type": "openinghours",
            "Content": "11h - 14h30, 17h - 22h30"
          }
        ],
        "Description": "S",
        "IsAdvertisement": false
      },
      {
        "Meta": [
          {
            "Type": "address",
            "Content": "515 Siming Nan Lu, Siming District, Xiamen"
          },
          {
            "Type": "phone",
            "Content": "+86 592 2085 908"
          },
          {
            "Type": "www",
            "Content": null
          }
        ]
      }
    ]
  }
]

,但我真的不想选择部分。我只需要ISO,图像,描述,视频

var arrivalGuidelist =等待_technedbcontext.arrivalguide .Select(x =&gt; new {x.iso,x.Image,x.image,x.Description,x.Description,x.video})) .tolistasync();

,但是我有同样的错误。为什么我也从此查询中得出上述错误。我没有选择。 那为什么会发生此错误?

system.text.json.jsonexception:JSON值无法转换为System.String。路径:$ [9] .pointsofintests [3] .meta [9] .content |亚麻布:0 | Bytepositioninline:103060。

I am working a asp.net core project with Postgresql.

I wrote a query to get a list ArrivalGuide .

// this is working fine
var x = await _catalogDbContext.Destinations.ToListAsync();  

// But I got error for this query
var y = await _catalogDbContext.ArrivalGuide.ToListAsync();  

I got this error:

fail: Microsoft.EntityFrameworkCore.Query[10100]

An exception occurred while iterating over the results of a query for context type 'Techne.TravelHub.Data.CatalogDbContext'.

System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $[9].PointsOfInterests[3].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 103060.

System.InvalidOperationException: Cannot get the value of a token type 'StartObject' as a string.

at System.Text.Json.Utf8JsonReader.GetString()
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnRead(ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
--- End of inner exception stack trace ---
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& readStack, Utf8JsonReader& reader, Exception ex)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at Npgsql.TypeHandlers.JsonHandler.Read[T](NpgsqlReadBuffer buf, Int32 byteLen, Boolean async, FieldDescription fieldDescription)

Entity classes:

public class ArrivalGuide
{
     public List<DestinationSection> Sections { get; set; }
     // removed rest
}

public class DestinationSection
{
    public List<PointsOfInterest> PointsOfInterests { get; set; }
    // removed rest
}

public class PointsOfInterest
{
    public List<PointOfInterestMeta> Meta { get; set; }
}

public class PointOfInterestMeta
{
    public string Type { get; set; }
    public string Content { get; set; }
}

Configuration:

builder.Property(i => i.Sections)
            .HasColumnType("jsonb");
    
builder.Ignore(i => i.Video);

Why does this error occur?

Edit: sections - jsonb

[
  {
    "Iso": "eating",
    "PointsOfInterests": [
      {
        "Meta": [
          {
            "Type": "address",
            "Content": "1A Bailuzhou Dong Lu, Siming District, Xiamen"
          },
          {
            "Type": "phone",
            "Content": "+86 592 5330 888"
          },
          {
            "Type": "www",
            "Content": null
          },
          {
            "Type": "openinghours",
            "Content": "11h - 14h30, 17h - 22h30"
          }
        ],
        "Description": "S",
        "IsAdvertisement": false
      },
      {
        "Meta": [
          {
            "Type": "address",
            "Content": "515 Siming Nan Lu, Siming District, Xiamen"
          },
          {
            "Type": "phone",
            "Content": "+86 592 2085 908"
          },
          {
            "Type": "www",
            "Content": null
          }
        ]
      }
    ]
  }
]

BUT I really don't want to select sections. I need only Iso, Image, Description, video

var arrivalGuideList = await _techneDbContext.ArrivalGuide .Select(x => new { x.Iso, x.Image, x.Description, x.Video }).ToListAsync();

But I got same error. Why I got the above mentioned error fro this query too. I did not select sections.
Then why this error occurs?

System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $[9].PointsOfInterests[3].Meta[9].Content | LineNumber: 0 | BytePositionInLine: 103060.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文