ConvertFrom-Json最大长度 [英] ConvertFrom-Json max length
问题描述
在转换大小超过2MB的JSON字符串时,使用PowerShell v3时出现问题. PowerShell使用的JSON序列化程序的默认限制设置为2MB,这说明了错误.
I have a problem using PowerShell v3 when converting JSON strings over 2MB in size. The default limit in JSON serializer used by PowerShell is set to 2MB which explains the error.
但是,当我在较小的集合上使用ConvertFrom-Json对对象进行反序列化时(我得到了具有越来越大的内部集合的各种数据对象,但这些是相同的对象),它返回的是非常漂亮的对象,它具有我可以轻松访问的所有属性.
However when I deserialize object using ConvertFrom-Json on a smaller set (I got various data objects with smaller and bigger internal collections but those are the same objects) it returns very nice object with all properties which I can easily access.
为克服序列化程序的限制,我尝试手动反序列化数据:
To overcome the limitations of the serializer I tried to deserialize data by hand:
$jsser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$jsser.MaxJsonLength = $jsser.MaxJsonLength * 10
$jsser.RecursionLimit = 99
$outObject = $jsser.DeserializeObject($json)
对象看起来不同,似乎内部集合未反序列化,当我尝试执行属性时,它们返回空结果.
The object looks differently it seems that internal collections were not deserialized and when I try to execute properties they return empty results.
我的问题:
-
假设是
ConvertFrom-Json
进行了一些附加的魔术处理或以某种方式在序列化之前为对象创建了模板.知道如何复制吗?
Assumption is
ConvertFrom-Json
does some additional magic or somehow creates a template for the object before serialization. Any idea how to replicate it?
我得到的对象始终是PSCustomObject
;如果我得到要通过ConvertFrom-Json
设置的对象,是否可以在JsonSerializer中将其用作对象类型?
The object I get is always a PSCustomObject
; if I get the object I want setup by ConvertFrom-Json
is there anyway to use it as object type in JsonSerializer?
推荐答案
我遇到了同样的问题,并能够像这样解决它:
I had the same problem and was able to solve it like this:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$jsonserial.MaxJsonLength = 67108864
$Obj = $jsonserial.DeserializeObject($CourseTypesResponse)
您可以使用$jsonserial.MaxJsonLength
来操纵MaxJsonLength属性
You can use $jsonserial.MaxJsonLength
to manipulate the MaxJsonLength property
来源: 查看全文