将自定义对象添加到JObject时出错

时间:2014-09-24 14:01:52

标签: c# .net json serialization json.net

这些是我的课程:

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.

1 个答案:

答案 0 :(得分:4)

您必须使用某种JToken作为JProperty的值(至少在复杂类型的情况下)。您可以使用FromObject

轻松获取其中一个
JProperty property1 = new JProperty("Details", JToken.FromObject(request));