Issue
I'm sending a field of array in multipart/form-data form to server, but when I call the variable in that array, it gives me an error.
Code
MultipartFormDataContent multiContent = new MultipartFormDataContent();
multiContent.Headers.ContentType.MediaType = "multipart/form-data";
string email = "[email protected]";
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");
Error
"\"properties must be a `array` type, but the final value was: `null` (cast from the value `\\\"[{\\\"propertyName\\\": \\\"email\\\", \\\"propertyValue\\\": [email protected], \\\"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()`\""
UPDATE
public class Item
{
public string name { get; set; }
public string value { get; set; }
}
Solution
With the help of @jimenemex and @colex-msft solution, I solved it.
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);
Answered By - Stavrogin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.