使用具有object数组属性的json对象从powershell调用rest方法

使用具有object数组属性的json对象从powershell调用rest方法,json,rest,powershell,serialization,Json,Rest,Powershell,Serialization,我试图模仿powershell中webapi调用主体中的c#类定义,以便可以使用表示为json的对象调用rest方法。我试图实现的json结构示例如下: '{ "Name" : "Test" , "Items" : [{"Key" : "Test", "Value" : "t2"},{"Key" : "Test2", "Value" : "t3"}] }' 当设置为postman中rest调用的主体时,这个json可以工作,如果我使用它调用powershell invokere

我试图模仿powershell中webapi调用主体中的c#类定义,以便可以使用表示为json的对象调用rest方法。我试图实现的json结构示例如下:

'{
    "Name" : "Test" ,
    "Items" : [{"Key" : "Test", "Value" : "t2"},{"Key" : "Test2", "Value" : "t3"}]
}'
当设置为postman中rest调用的主体时,这个json可以工作,如果我使用它调用powershell invokerest方法,那么webapi端的所有字段都是空的

下面是我目前在powershell中对此的尝试,请注意,Items实际上是一个键值对对象的列表。使用下面的代码,我只得到items中的1个item,名称和值为null

$spec = @{
    Name = 'Test' 
    Items = ,{Name = 'Test', Value = 't2'}, {Name = 'Test2', Value = 't3'}
}

invoke-restmethod $someurl  -Method:POST -body $spec | Write-Host
我很确定我在powershell对象上的项的数组声明中遗漏了一些重要的内容

以下是其余方法:

   [HttpPost]
    [Route("addorupdateitem")]
    public void Add([FromBody] ItemConfigurationDto itemconfig)
    {
        var serializeObject = Newtonsoft.Json.JsonConvert.SerializeObject(context);
        Debug.WriteLine(serializeObject);
    }
还有这些东西

public class ItemConfigurationDto
{
    public string Name { get; set; }
    public List<Item> Items { get; set; } = new List<Item>();
}

public class Item
{
    public string Name { get; set; }
    public string Value { get; set; }
}
公共类ItemConfigurationDto
{
公共字符串名称{get;set;}
公共列表项{get;set;}=new List();
}
公共类项目
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
}

解决了这个问题。我必须设置-ContentType'application/json'并在将对象添加到正文之前将其转换为json。

Items=@{Key='Test';Value='t2'},@{Key='Test2';Value='t3'}
@PetSerAl如果一个对象也不起作用,我会得到一个单独的项,名称和值都为null