这些是我的类(class):
public class RequestEntity
{
public int Category { get; set; }
public List<string> Types { get; set; }
public List<Parameters> parameters { get; set; }
}
public class Parameters
{
public string Name { get; set; }
public string Type { get; set; }
public bool IsRecent { get; set; }
}
将值设置为:
List<RequestEntity> request = new List<RequestEntity>();
现在我需要创建一个具有 2 个属性的
JObject
。JObject requestObject = new JObject();
JProperty property1 = new JProperty("Details", request);
JProperty property2 = new JProperty("SpanInDays", 10);
requestObject.Add(property1);
requestObject.Add(property2);
JProperty property1 = new JProperty("Details", request);
行给了我以下错误。Could not determine JSON object type for type DAL.Entity.RequestEntity.
最佳答案
您必须使用某种 JToken
作为 JProperty
的值(至少在复杂类型的情况下)。您可以使用 FromObject
轻松获得其中之一:
JProperty property1 = new JProperty("Details", JToken.FromObject(request));
关于c# - 将自定义对象添加到 JObject 时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26018906/