简体   繁体   English

Powershell:将名称和值数组高性能转换为可解析格式 - 我怎样才能使它更快

[英]Powershell: High Performing conversion of Name and Value Array into Parseable format - How can I make this Faster

How can I make my code a lot more performant when I wish to make an easily/fast parseable object/PSCustomObject from a JSON payload $JSON ?当我希望从 JSON 有效负载$JSON制作一个容易/快速的可解析对象/PSCustomObject 时,如何使我的代码性能更高?

An example of the structure of the PAYLOAD I receive is:我收到的 PAYLOAD 结构的一个示例是:

[
    {
        "name":  "system.enablenetflow",
        "value":  "false"
    },
    {
        "name":  "system.deviceGroupId",
        "value":  "186,3060"
    },
    {
        "name":  "system.prefcollectorid",
        "value":  "144"
    },
    {
        "name":  "system.collectorplatform",
        "value":  "windows"
    }
]

As you can see its in a very annoying format.如您所见,它的格式非常烦人。

Note that the payloads I attempt to parse are much larger and variable in count from 500 of these Name/Value objects to 50000, rather than just the 4 listed above.请注意,我尝试解析的有效负载要大得多,并且数量从 500 个这些名称/值对象到 50000 个不等,而不仅仅是上面列出的 4 个。

########################################################################### ################################################# #########################

MY GOAL我的目标

To have this turn into a key:value pair scenario for easier parsing later把它变成一个键值对场景,以便以后更容易解析

NOT This :不是这个

With the JSON I have to do $JSON.where({$_.name -eq "system.enablenetflow"}).value使用 JSON 我必须做$JSON.where({$_.name -eq "system.enablenetflow"}).value

YES THIS :是的

I want the end state to be that the new variable $obj I create will let me get the value with $obj."system.enablenetflow"我希望结尾 state 是我创建的新变量$obj将让我获得$obj."system.enablenetflow"

########################################################################### ################################################# #########################

MY CURRENT ATTEMPT THAT IS SUPER SLOW我目前的尝试非常慢

I did the following:我做了以下事情:

  1. Create an Empty PSCustomObject and saved it as variable $obj创建一个 Empty PSCustomObject 并将其保存为变量$obj
  2. Did a foreach method on the $JSON variable which iterated through the JSON Array对遍历 JSON 数组的$JSON变量执行了 foreach 方法
  3. Add-Member to $obj with setting the 'name' as PropertyName and 'value' as PropertyValue将“名称”设置为 PropertyName,将“值”设置为 PropertyValue,将成员添加到$obj

Heres a sample of my code:这是我的代码示例:

$obj = [PSCustomObject] @{}
$json.foreach({
   $thisitem = $_
   $obj | Add-member -NotePropertyName $($thisitem.name) -NotePropertyValue $($thisitem.name)
})

HOW CAN I MAKE THIS FASTER?我怎样才能让它更快?

# Sample input JSON.
$json =  @'
[
    {
        "name":  "system.enablenetflow",
        "value":  "false"
    },
    {
        "name":  "system.deviceGroupId",
        "value":  "186,3060"
    },
    {
        "name":  "system.prefcollectorid",
        "value":  "144"
    },
    {
        "name":  "system.collectorplatform",
        "value":  "windows"
    }
]
'@

# Initialize the (ordered) result hashtable.
$result = [ordered] @{}

# Note: In PowerShell (Core) 7+, add -AsHashTable to the ConvertFrom-Json
#       call for additional performance gain, combined with -AsArray,
#       in which case you don't need the `(...)` around the call anymore.
foreach ($element in (ConvertFrom-Json $json)) {
  $result[$element.name] = $element.value
}

The above creates an (ordered) hashtable instead of a [pscustomobject] instance - especially if the latter are iteratively constructed via Add-Member calls.上面创建了一个(有序的)哈希表而不是一个[pscustomobject]实例 - 特别是如果后者是通过Add-Member调用迭代构造的。

Hashtables are lighter-weight and faster to construct than [pscustomobject] instances.哈希表比[pscustomobject]实例更轻量且构建速度更快。

Using a foreach loop rather than processing the ConvertFrom-Json output in a pipeline via ForEach-Object also speeds up processing.使用foreach循环而不是通过ForEach-Object管道中处理ConvertFrom-Json output 也可以加快处理速度。

PowerShell allows you to use the familiar dot notation also with hashtables; PowerShell 允许您在哈希表中也使用熟悉的点表示法; so, for instance, after running the above, you'll get:因此,例如,在运行上述内容后,您将获得:

PS> $result.'system.collectorplatform'
windows

If you do need $result to be a [pscustomobject] instance, you can simply cast the fully populated hashtable to that type:如果您确实需要$result作为[pscustomobject]实例,您可以简单地将完全填充的哈希表转换为该类型:

PS> $obj = [pscustomobject] $result; $obj.'system.collectorplatform'
windows

暂无
暂无

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

相关问题 如何使用javascript在对象数组中使json对象的字段名称具有值? - How can I make my json object's field name a value with in an object array with javascript? 如何将相同名称的值格式化为数组 - How to format the same name value into an array 如何格式化名称具有特殊字符的数组(json 格式)中的元素? - How can I format an element in array (json format) which its name has special characters? 如何在Swift 3中使用Alamofire制作并发送字典数组(JSON格式)发送到服务器 - How Can I make & send array of dictionary (JSON Format ) send to server using Alamofire in swift 3 如何为名称设置范围? - How can I make a range for a name? 如何使此脚本运行更快 - How do i make this script run faster 如何使用Jackson将“名称”“值”对的JSON数组反序列化为Pojo - How can I deserialize a JSON array of “name” “value” pairs to a Pojo using Jackson 如何在将用于引用Pouchdb中的JSON名称的函数中传递数组值? - How can I pass an Array value in a function that will be used reference a JSON name in Pouchdb? 如何使用 JSON.NET 将 JSON 数组中的 object 名称用作属性值? - How can I use the object name in a JSON array as a property value using JSON.NET? 反应 axios 后以数组格式生成 json。 如何将其转换为“名称”:“值”类型格式 - react axios post generates json in array format. How to convert this to “name”:“value” type format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM