我使用api请求我根据我发送的所有参数的值使用一些哈希算法来计算我的请求的签名。如果签名错误,我的请求将被拒绝。因此,对我来说,列出我发送的所有参数非常重要。
我有一个复杂的对象,我试图发送:
public class InboundShipmentPlan
{
public InboundShipmentPlan()
{
InboundShipmentPlanRequestItems = new List<InboundShipmentPlanRequestItem>();
}
public Address ShipFromAddress { get; set; }
public string ShipToCountryCode { get; set; }
public string LabelPrepPreference { get; set; }
public List<InboundShipmentPlanRequestItem> InboundShipmentPlanRequestItems { get; set; }
}
public class Address
{
public string Name { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string DistrictOrCounty { get; set; }
public string CountryCode { get; set; }
public string PostalCode { get; set; }
}
public class InboundShipmentPlanRequestItem
{
public string Prop1{ get; set; }
public string Prop2 { get; set; }
public string Condition { get; set; }
public int Quantity { get; set; }
}
这就是我&#39;做:
InboundShipmentPlanRequestItem item = new InboundShipmentPlanRequestItem
{
Prop1 = "My",
Prop2 = "Zorro",
Quantity = 2
};
Address shipmentPlanShipFromAddress = new Address
{
AddressLine1 = "25 Courtfield Garden",
City = "London",
CountryCode = "UK",
PostalCode = "SW5 OPG",
Name = "The Naddler"
};
InboundShipmentPlan shipmentPlan = new InboundShipmentPlan
{
ShipToCountryCode = "US",
ShipFromAddress = shipmentPlanShipFromAddress
};
shipmentPlan.InboundShipmentPlanRequestItems.Add(item);
var client = new RestClient(endpointAdresss) {Authenticator = new CustomAuthenticator()};
var request = new RestRequest(Method.POST) {Resource = "SomeAction/2010-10-01"};
request.RequestFormat = DataFormat.Json;
request.AddParameter("Action", "CreateInboundShipmentPlan");
//var serializedObject = JsonConvert.SerializeObject(shipmentPlan);
request.AddBody(shipmentPlan);
var response = client.Execute(request);
我正在验证身份验证器中计算签名。我的问题是request.AddBody没有将我的JSON对象属性添加到参数列表(request.Parameters),因此我的签名是错误的。方法request.AddObject会将它们添加到请求参数列表中,但此方法的问题是该对象未被序列化,因此复杂属性(Address等)具有如下值:System.Collection.Generic .LIST。
我该如何解决这个问题?感谢
更新
我发现当我使用AddJsonObject时,它实际上添加了一个参数,其名称为&#34; application / json&#34;,value是JSON字符串。但是,我需要的是,如果我有一个包含2个字符串属性的复杂类型和一个属性,它是长度为n的复杂类型的列表,则会向请求中添加2 + n个单独的参数,而不仅仅是一个JSON字符串。
一个解决方法是解析JSON字符串并将其转换为键值字典,但我还没有找到一个通用的实现,并且不确定是否值得深入研究它。到目前为止我找到的所有解决方案都是基于这样一个事实,即作者事先知道他试图压扁的JSON对象的结构。
答案 0 :(得分:0)
您可以避免使用 Generics 进行序列化,而是使用 Arrays 。
您也可以使用 DataContractSerializer 。
var ser = new DataContractJsonSerializer(shipmentPlan.GetType());
var stream = new MemoryStream();
ser.WriteObject(stream, shipmentPlan);
var result = Encoding.Default.GetString(stream.ToArray());
执行结果后将是正确的JSON字符串:
{"InboundShipmentPlanRequestItems":[{"ASIN":"Zorro","Condition":null,"Quantity":2,"SellerSKU":"My"}],"LabelPrepPreference":null,"ShipFromAddress":{"AddressLine1":"25 Courtfield Garden","AddressLine2":null,"City":"London","CountryCode":"UK","DistrictOrCounty":null,"Name":"The Naddler","PostalCode":"SW5 OPG"},"ShipToCountryCode":"US"}
使用它可以毫无问题地将对象传递给服务器端。
如果你确实需要某些散列算法的对象的所有参数,那么你应该在你的对象中创建一个方法来生成你可以作为参数添加的散列序列。