【问题标题】:JSON Parsing Issue - 'S' is an invalid escapable character within a JSONJSON 解析问题 - “S”是 JSON 中无效的可转义字符
【发布时间】:2021-06-13 11:21:49
【问题描述】:

我在appsettings 文件中有以下 JSON,它有一个连接字符串。

 {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "ApplicationInsights": {
        "InstrumentationKey": "8eje88w0-1509-4ae0-b58c-1a7ee5b9c7e4"
      },
       "ConnectionStrings": {
        "DefaultConnString": "Server=MYPC\SQLEXPRESS;Database=CoreService.Mark;Trusted_Connection=True;"
      },
      "AllowedHosts": "*"
    }

但是在执行程序时,我不断收到以下错误。我试图找到任何错误,但找不到任何错误。请指教

Unhandled exception. System.FormatException: Could not parse the JSON file.
 ---> System.Text.Json.JsonReaderException: 'S' is an invalid escapable character within a JSON string. The string should be correctly escaped. LineNumber: 10 | BytePositionInLine: 42

【问题讨论】:

  • 反斜杠必须转义。
  • 要么使用正斜杠,要么使用两次反斜杠
  • 来自 Json.org 的 Json 字符串定义:json.org/img/string.png

标签: c# json .net appsettings


【解决方案1】:

在您的ConnectionStrings. DefaultConnString 字段中,您正在转义S(在SQLExpress; 之前)。正如错误所暗示的那样,您不能这样做。

您可以通过在预先存在的转义字符前添加另一个转义字符(反斜杠,\)来解决此问题。反斜杠是可转义的字符,最终结果将是想要的字符,您将在该位置获得一个反斜杠。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多