benjamin54
benjamin54

Reputation: 1300

Error while adding custom object to JObject

These are my classes:

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; }
}

After setting value to:

List<RequestEntity> request = new List<RequestEntity>();

Now I need to create a JObject with 2 properties.

JObject requestObject = new JObject();
JProperty property1 = new JProperty("Details", request);
JProperty property2 = new JProperty("SpanInDays", 10);
requestObject.Add(property1);
requestObject.Add(property2);

The line JProperty property1 = new JProperty("Details", request); is giving me following error.

Could not determine JSON object type for type DAL.Entity.RequestEntity.

Upvotes: 4

Views: 1730

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

You must use some kind of JToken as the value of a JProperty (at least in the case of complex types). You can easily get one of those by using FromObject:

JProperty property1 = new JProperty("Details", JToken.FromObject(request));

Upvotes: 6

Related Questions