当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Test-Json (Microsoft.PowerShell.Utility)

作者:精品下载站 日期:2024-12-14 02:03:14 浏览:14 分类:玩电脑

Test-Json (Microsoft.PowerShell.Utility)


Test-Json

模块 :Microsoft.PowerShell.Utility

测试字符串是否是有效的 JSON 文档

句法

Test-Json
    [-Json] <String>
    [<CommonParameters>]
Test-Json
    [-Json] <string>
    [-Schema] <string>
    [<CommonParameters>]
Test-Json
    [-Json] <string>
    [-SchemaFile] <string>
    [<CommonParameters>]
Test-Json
    [-Path] <string>
    [<CommonParameters>]
Test-Json
    [-Path] <string>
    [-Schema] <string>
    [<CommonParameters>]
Test-Json
    [-Path] <string>
    [-SchemaFile] <string>
    [<CommonParameters>]
Test-Json
    [-LiteralPath] <string>
    [<CommonParameters>]
Test-Json
    [-LiteralPath] <string>
    [-Schema] <string>
    [<CommonParameters>]
Test-Json
    [-LiteralPath] <string>
    [-SchemaFile] <string>
    [<CommonParameters>]

描述

Test-Json cmdlet 测试字符串是否是有效的 JavaScript 对象表示法 (JSON) 文档,并且可以选择根据提供的架构验证该 JSON 文档。

然后,可以将经过验证的字符串与 ConvertFrom-Json cmdlet 一起使用,将 JSON 格式的字符串转换为 JSON 对象,该对象可以在 PowerShell 中轻松管理或发送到访问 JSON 输入的另一个程序或 Web 服务。

许多网站使用 JSON 而不是 XML 来序列化数据,以便在服务器和基于 Web 的应用程序之间进行通信。

此 cmdlet 是在 PowerShell 6.1 中引入的

示例

示例 1:测试对象是否是有效的 JSON

此示例测试输入字符串是否是有效的 JSON 文档。

'{"name": "Ashley", "age": 25}' | Test-Json

True

示例 2:根据提供的模式测试对象

此示例采用包含 JSON 架构的字符串,并将其与输入字符串进行比较。

$schema = @'
{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "The Root Schema",
  "required": [
    "name",
    "age"
  ],
  "properties": {
    "name": {
      "$id": "#/properties/name",
      "type": "string",
      "title": "The Name Schema",
      "default": "",
      "examples": [
        "Ashley"
      ],
      "pattern": "^(.*)$"
    },
    "age": {
      "$id": "#/properties/age",
      "type": "integer",
      "title": "The Age Schema",
      "default": 0,
      "examples": [
        25
      ]
    }
  }
}
'@
'{"name": "Ashley", "age": "25"}' | Test-Json -Schema $schema

Test-Json:
Line |
  35 |  '{"name": "Ashley", "age": "25"}' | Test-Json -Schema $schema
     |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~
     | The JSON is not valid with the schema: Value is "string" but should be "integer" at '/age'
False

在此示例中,我们收到错误,因为架构需要 age 为整数,但我们测试的 JSON 输入使用字符串值。

有关更多信息,请参阅 JSON 架构。

示例 3:根据文件中的模式测试对象

JSON 模式可以使用 $ref 关键字引用定义。 $ref 可以解析为引用另一个文件的 URI。 SchemaFile 参数接受 JSON 架构文件的文字路径,并允许根据此类架构验证 JSON 文件。

在此示例中,schema.json 文件引用 definitions.json

Get-Content schema.json

{
  "description":"A person",
  "type":"object",
  "properties":{
    "name":{
      "$ref":"definitions.json#/definitions/name"
    },
    "hobbies":{
      "$ref":"definitions.json#/definitions/hobbies"
    }
  }
}

Get-Content definitions.json

{
  "definitions":{
    "name":{
      "type":"string"
    },
    "hobbies":{
      "type":"array",
      "items":{
        "type":"string"
      }
    }
  }
}

'{"name": "James", "hobbies": [".NET", "Blogging"]}' | Test-Json -SchemaFile 'schema.json'

True

有关更多信息,请参阅构建复杂模式。

参数

-Json

指定要测试有效性的 JSON 字符串。输入包含该字符串的变量,或键入获取该字符串的命令或表达式。您还可以通过管道将字符串传递给 Test-Json

Json 参数是必需的。

类型 :

String

位置:

0

默认值:

None

必需的:

True

接受管道输入:

True

接受通配符:

False

-LiteralPath

指定 JSON 文件的路径。 LiteralPath 的值完全按照其键入的方式使用。没有字符被解释为通配符。如果路径包含转义字符,请将其用单引号引起来。单引号告诉 PowerShell 不要将任何字符解释为转义序列。

此参数是在 PowerShell 7.4 中添加的。

类型 :

String

别名:

PSPath, LP

位置:

0

默认值:

None

必需的:

True

接受管道输入:

True

接受通配符:

False

-Path

指定 JSON 文件的路径。此 cmdlet 获取指定位置处的项目。允许使用通配符,但模式必须解析为单个文件。

此参数是在 PowerShell 7.4 中添加的。

类型 :

String

位置:

0

默认值:

None

必需的:

True

接受管道输入:

True

接受通配符:

True

-Schema

指定用于验证 JSON 输入的架构。如果通过,Test-Json 会验证 JSON 输入是否符合 Schema 参数指定的规范,并仅在输入符合时才返回 $true到提供的架构。

有关更多信息,请参阅 JSON 架构。

类型 :

String

位置:

1

默认值:

None

必需的:

True

接受管道输入:

False

接受通配符:

False

-SchemaFile

指定用于验证 JSON 输入的架构文件。使用时,仅当 JSON 输入符合 SchemaFile 参数指定的文件中定义的架构时,Test-Json 才会返回 $true

有关更多信息,请参阅 JSON 架构。

类型 :

String

位置:

1

默认值:

None

必需的:

True

接受管道输入:

False

接受通配符:

False

输入

字符串

您可以通过管道将 JSON 字符串传递到此 cmdlet。

输出

布尔值

如果 JSON 有效,则此 cmdlet 返回 $true,否则返回 $false

笔记

从 PowerShell 6 开始,PowerShell 使用 Newtonsoft.Json 程序集来执行 JSON 函数。 Newtonsoft 的实现包括对 JSON 标准的多个扩展,例如对注释的支持和单引号的使用。有关功能的完整列表,请参阅 Newtonsoft 文档:https://www.newtonsoft.com/json。

从 PowerShell 7.4 开始,Test-Json 使用 System.Text.Json 进行 JSON 解析,使用 JsonSchema.NET 进行架构验证。经过这些更改,Test-Json

  • 不再支持 Draft 4 架构
  • 仅支持严格符合的 JSON

有关 Newtonsoft.Json 和 System.Text.Json 之间差异的完整列表,请参阅从 Newtonsoft.Json 迁移到 System.Text.Json 中的差异表。

有关 JSON 架构规范的更多信息,请参阅 JSON-Schema.org 上的文档。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯