我想使用Restsharp库通过API发布以下JSON
{
"id" : "customerID",
"name" : "customerName",
"customKeys" : {
"dataA" : "{\"keyA\": \"valueA\", \"keyB\": \"valueB\"}",
"dataB" : "{\"keyA\": \"valueA\", \"keyB\": \"valueB\"}"
}
}
实际上,我编写了以下代码来创建Customer对象,并且一切都在最后一个字段customKeys旁边正常工作。...
public class Customer
{
public string id { get; set; }
public string name { get; set; }
public List<Dictionary<string, object>> customKeys { get; set; }
}
Customer customer = new Customer
{
id = id,
name = customerName,
customKeys = ????????????????
};
string json = JsonConvert.SerializeObject(customer);
RestRequest request = newRestRequest("someEndpoint",Method.POST);
request.AddJsonBody(customer);
var response = client.Execute(request);
我只是想确定在以下情况下应该使用哪种数据类型。 我应该使用词典词典还是仅使用ListOfDictionaries?或者,也许有更好,更轻松的方法来进行POST呼叫?
答案 0 :(得分:0)
如果您的词典除了位置之外还有特殊含义,您可以使用词典词典。 否则,最好使用词典列表。
答案 1 :(得分:0)
您可以尝试以下方法:
public class CustomKeys
{
public string dataA { get; set; }
public string dataB { get; set; }
}
public class Customer
{
public string id { get; set; }
public string name { get; set; }
public List<CustomKeys> customKeys { get; set; }
}
Customer customer = new Customer
{
id = id,
name = customerName,
customKeys = new List<CustomKeys>()
{
dataA ="dataA",
dataB ="dataB "
};
};