【问题标题】:Properties must be a 'array' type, but the final value was: 'null' in C#属性必须是“数组”类型,但最终值为:C# 中的“null”
【发布时间】:2021-08-28 22:25:23
【问题描述】:

我正在将multipart/form-data 形式的数组字段发送到服务器,但是当我调用该数组中的变量时,它给了我一个错误。

代码

MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Headers.ContentType.MediaType = "multipart/form-data";
string email = "mail@example.com";
string firstName = "Andrie";
string lastName = "Tarkovsky";

var fields = "[{\"propertyName\": \"email\", \"propertyValue\": " + email + ", \"propertyLabel\": \"Email\"}, {\"propertyName\": \"firstName\", \"propertyValue\": " + firstName + ", \"propertyLabel\": \"First Name\"}, {\"propertyName\": \"lastName\", \"propertyValue\": " + lastName + ", \"propertyLabel\": \"Last Name\"}]";

multiContent.Add(new StringContent(fields), "properties");

错误

"\"properties must be a `array` type, but the final value was: `null` (cast from the value `\\\"[{\\\"propertyName\\\": \\\"email\\\", \\\"propertyValue\\\": mail@example.com, \\\"propertyLabel\\\": \\\"Email\\\"}, {\\\"propertyName\\\": \\\"firstName\\\", \\\"propertyValue\\\": Andrie, \\\"propertyLabel\\\": \\\"First Name\\\"}, {\\\"propertyName\\\": \\\"lastName\\\", \\\"propertyValue\\\": Tarkovsky, \\\"propertyLabel\\\": \\\"Last Name\\\"}]\\\"`).\\n If \\\"null\\\" is intended as an empty value be sure to mark the schema as `.nullable()`\""

更新

public class Item
{
    public string name { get; set; }
    public string value { get; set; }
}

【问题讨论】:

  • 请分享“multiContent”的类型
  • 您需要添加更多代码,例如multiContent的类型。 field 属性在我看来更像 JSON?
  • 添加在上面,请看一下..谢谢
  • “它给了我一个错误”——“它”是谁?是编译器错误吗?例外?服务器响应错误?
  • 您不应该尝试手动创建 JSON。

标签: c# .net xamarin xamarin.forms


【解决方案1】:

您不应该尝试手动创建 JSON。

如果您没有将这些变量作为对象的具体类,则创建一个匿名类,然后您可以使用Newtonsoft's JsonConvert.SerializeObject() 将对象转换为 JSON。

using Newtonsoft.Json;
...

// string email = "mail@example.com";
// string firstName = "Andrie";
// string lastName = "Tarkovsky";

var obj = new 
{
    email = "mail@example.com",
    firstName = "Andrie",
    lastName = "Tarkovsky"
};

multiContent.Add(new StringContent(JsonConvert.SerializeObject(obj)), "properties");

如果它们是“动态”值,则创建一个表示每个值的对象。

public class DynamicObject
{
    public string PropertyName { get; set; }
    public string PropertyValue { get; set; }
}

List<DynamicObject> objs = new List<DyanmicObject>(); // Or however you have the list

// Add to the list

JsonConvert.SerializeObject(objs);

【讨论】:

  • 基本上它们是动态字段,例如电子邮件、名字、姓氏.. 要求是我必须像这样[{"propertyName": item.name, "propertyValue": item.Value}]; 将它们发送到服务器,其中item.nameemail, firstName, lastName..
  • 之前我试过这个Value.ToList().Select(item =&gt; new { item.name, item.Value }).ToList();,但唯一的问题是它创建了一个像这样[{"name": item.name, "Value": item.Value}]的对象,我想要像这样[{"propertyName": item.name, "propertyValue": item.Value}]
  • 注意: email, firstName, lastName 是动态的,可能更少或更多,我从服务器获取它们。
  • 先生,我已经创建了定义 name, value, label 的类。item.name 包含动态字段的名称,例如email, firstName, lastNameitem.value 存储它的值,item.label 存储字段的标签。
  • 现在我只想知道如何将这样的数据发送到服务器[{"propertyName": item.name, "propertyValue": item.Value}];
【解决方案2】:

Jimenemex 的代码应该可以正常工作。

只需在objs 中填充数据并将其序列化为json,然后发送到服务器。

public class Item
{
    public string propertyName{ get; set; }
    public string propertyValue{ get; set;}
}

List<Item> objs = new List<Item>();
foreach (var p in item.GetType().GetProperties()) {
   objs.Add(new DynamicObject { propertyName = p.Name, propertyValue =(string)p.GetValue(item) });
}

var json = JsonConvert.SerializeObject(objs);
multiContent.Add(new StringContent(json), "properties");

【讨论】:

  • 感谢您的回复,但正如我在上面的 cmets 中提到的,我不知道有多少字段(它们是动态的)。我从 public class Item {}
  • objs.Add(new Item { propertyName = Item.name, propertyValue = item.value }); 我想做这样的事情..我怎么能做到这一点?...谢谢
  • 你的意思是Item 类是动态的吗?我们不知道它的字段?
  • 请看上面的更新部分..这是你要的吗?
  • 我觉得你可以使用`reflection`来获取动态属性名及其值。
【解决方案3】:

@jimenemex@colex-msft解决方案的帮助下,我解决了。

public class Item
{
    public string name { get; set; }
    public string value { get; set; }
}

public class DynamicObject
{
    public string PropertyName { get; set; }
    public string PropertyValue { get; set; }
}

List<Item> Obj = new List<Item>();
List<DynamicObject> Objs = new List<DynamicObject>();

foreach (var p in value.ToList())
{
    Objs.Add(new DynamicObject { PropertyName = p.name, PropertyValue = p.value });
}

var json = JsonConvert.SerializeObject(Objs);

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 2020-11-04
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2017-05-06
    • 2020-09-24
    • 2021-06-11
    • 2021-08-06
    相关资源
    最近更新 更多