반응형
해석된 JSON에 PowerShell을 추가하는 방법
PowerShell을 사용하여 구문 분석된 JSON에 무언가를 추가하고 싶습니다.내 코드:
function ConvertFromJson([string]$file)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$jsoncontent = Get-Content $file
$jsonobj = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$global:json = $jsonobj.DeserializeObject($jsoncontent)
}
내 JSON:
{
"BlockA": {
"BlockB": {
"name": "BlockB",
"value": "Value_B"
},
}
BlockC를 이렇게 만들고 싶다.
{
"BlockA": {
"BlockB": {
"name": "BlockB",
"value": "Value_B"
},
"BlockC": {
"name": "BlockC",
"value": "Value_C"
},
}
나는 노력했다.
$json.BlockA.Add("BlockC", "")
그리고.
$json.BlockA.BlockC.Add("name", "BlockC")
다음 오류에서는 작동하지 않습니다.
추가 메서드는 없습니다.
할 수 있는 건 다 해봤는데([Add Method], [Add-Member] 사용)
추가: PS C:\Users\Develope7> $json.BlockA.BlockC | 멤버 가져오기
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB)
Contains Method bool Contains(string value)
CopyTo Method System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), i...
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normaliz...
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startI...
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizat...
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char padding...
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, s...
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringCom...
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower Method string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToString Method string ToString(), string ToString(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property System.Int32 Length {get;}
PowerShell 3.0/4.0을 사용하는 경우 Convert From-Json cmdlet을 사용하여 변환을 간소화할 수 있습니다.
그 밖에도 PS 또는 를 사용하고 있는 경우.Net Object Types, Add-Member cmdlet을 사용하여 임의의 속성을 추가할 수 있습니다.다음은 Json 블록을 기반으로 속성을 추가하는 방법을 보여 줍니다.
$json = @"
{
"BlockA": {
"BlockB": {
"name": "BlockB",
"value": "Value_B"
}
}
}
"@
$blockcvalue =@"
{
"name":"BlockC",
"value":"ValueC"
}
"@
$jobj = ConvertFrom-Json -InputObject $json
$jobj.BlockA | add-member -Name "BlockC" -value (Convertfrom-Json $blockcvalue) -MemberType NoteProperty
write-host (ConvertTo-Json $jobj)
이 오류가 발생하는 것은 $json이 실제로는 두 개체의 집합이기 때문입니다.하나는 조립품이고 다른 하나는 사전이다.어셈블리를 로드하는 선의 파이프 출력Out-Null
그걸 피하려고.예:
function ConvertFrom-Json([String]$sRawJson) {
[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") `
| Out-Null
$oJsSerializer = `
New-Object System.Web.Script.Serialization.JavaScriptSerializer
return $oJsSerializer.DeserializeObject($sRawJson)
}
$sBaseContent = @"
{
"BlockA": {
"BlockB": {
"name": "BlockB",
"value": "Value_B"
}
}
}
"@
$sBlockcContent = @"
{
"name": "BlockC",
"value": "Value_C"
}
"@
$jsonBaseObj = ConvertFrom-Json($sBaseContent)
$jsonBlockcObj = ConvertFrom-Json($sBlockcContent)
$jsonBaseObj.BlockA.Add("BlockC", $jsonBlockcObj)
$jsonBaseObj
언급URL : https://stackoverflow.com/questions/23720045/powershell-how-to-add-something-on-parsed-json
반응형
'sourcecode' 카테고리의 다른 글
React.js에서 리치 데이터 구조 편집 (0) | 2023.03.04 |
---|---|
AngularJS 형식 JSON 문자열 출력 (0) | 2023.03.04 |
JSON 개체에서 __type 속성을 직렬화하지 않는 방법 (0) | 2023.03.04 |
요소의 각 ng-반복 조건부 랩 항목(ng-반복 그룹 항목) (0) | 2023.03.04 |
키 이름을 몰라도 JSON 객체의 요소에 액세스합니다. (0) | 2023.03.04 |