gpt4 book ai didi

json - PowerShell 如何在解析的 JSON 上添加一些内容?

转载 作者:行者123 更新时间:2023-12-02 07:57:47 28 4
gpt4 key购买 nike

我想使用 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")

但它不适用于以下错误:

there is no add method

我尝试了所有我能做的(尝试添加方法,使用添加成员),但都失败了。

补充: PS C:\Users\Develop7> $json.BlockA.BlockC | PS C:\Users\Develop7> $json.BlockA.BlockC |获取成员(member)

   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,您可以使用 ConvertFrom-Json cmdlet 简化转换。 .

除此之外,如果您有 PS 或 .Net 对象类型,则 Add-Member cmdlet 允许您添加任意属性。下面展示了如何基于Json block 添加属性:

$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 - PowerShell 如何在解析的 JSON 上添加一些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720045/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com