简体   繁体   English

NewtonSoft JSON解析失败,因为JSON模式具有作为箭头功能的值,且不包含引号

[英]NewtonSoft JSON Parsing fails, since JSON Schema has a value as arrow function without quotes enclosed

I write an ASP.NET Core REST API. 我写了一个ASP.NET Core REST API。 The end point return the JSON. 终点返回JSON。 The API parse JSON schema red from a file. API会从文件中解析红色的JSON模式。 The JSON Schema has some value as arrow function as given below. JSON模式具有以下箭头功能的某些值。

The Newtonsoft unable parse the JSON Schema without quotes in arrow function in validation message as below. Newtonsoft无法解析JSON模式,而没有以下验证消息中的箭头功能中的引号。

      "ip": {
        "$id": "#/properties/ip",
        "type": "string",
        "title": "The Ip Schema",
        "default": "",
        "examples": [
          "111.123.789.654"
        ],
        "pattern": "^(\\d{1,3}\\.){3}\\d{1,3}$",
        "widget": {
          "formlyConfig": {
            "validation": {
              "messages": {
                "pattern": (error, field: FormlyFieldConfig) => `${ field.formControl.value } is not a valid IP Address`
              }
            }
          }
        }
      }

The following C# code fail, since JSON is not valid. 由于JSON无效,因此以下C#代码失败。

 var wraperobject = JObject.Parse(ui_schema);

If I add quotes like below as given below, the parsing works. 如果我添加如下所示的引号,则解析有效。 I need to send it to UI without quotes, otherwise all consumer need to do manipulation at client side. 我需要将其发送到不带引号的UI,否则所有消费者都需要在客户端进行操作。

Please give me a solution. 请给我一个解决方案。

  "ip": {
    "$id": "#/properties/ip",
    "type": "string",
    "title": "The Ip Schema",
    "default": "",
    "examples": [
      "111.123.789.654"
    ],
    "pattern": "^(\\d{1,3}\\.){3}\\d{1,3}$",
    "widget": {
      "formlyConfig": {
        "validation": {
          "messages": {
            "pattern": "(error, field: FormlyFieldConfig) => `${ field.formControl.value } is not a valid IP Address`"
          }
        }
      }
    }
  }

JSON should be able to have value as arrow function with quotes enclosed. JSON应该具有箭头功能,并带有引号。

I can send it as string but need to escape the entire JSON to make it as a string. 我可以将其作为字符串发送,但是需要转义整个JSON以使其成为字符串。 The UI side JSON parsing to be done. UI端JSON解析完成。

Please let me know if there best alternative. 请让我知道是否有最佳选择。

The value of a JSON property can be one of : object, array, string, number, "true", "false", or "null". JSON属性的值可以是 :对象,数组,字符串,数字,“ true”,“ false”或“ null”之一。

The value of "pattern" is none of those “模式”的值不属于那些

"pattern": (error, field: FormlyFieldConfig) => `${ field.formControl.value } is not a valid IP Address`

Since that value isn't valid for JSON, JSON.Net (or any other parser) cannot interpret it. 由于该值对于JSON无效,因此JSON.Net(或任何其他解析器)无法解释它。

When you quote your value, it becomes a valid string, which is why the parser can handle it. 引用值时,它变成有效的字符串,这就是解析器可以处理它的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM