Parsing Json .Net Web Api

asp.net-web-api2 c# entity-framework json rest

Question

I'm new with Web API 2 / Entity Framework 6 project, I'm making REST services, but for one specific service I'm going to receive (via Post) a JSON before making any CRUD operations over any entity of the model, (have to make some business validations over the data, add or complement some things and decide on wich entity to save), the JSON is:

{
    "head": {
        "action": "create",
        "object": "oneobject",
        "user": "theuser"
    },
    "object": {
        "name1": "a name 1",
        "name2": "a name 2",
        "description": "a description here"
    },
    "rule": [{
        "name": "any name",
        "value": "any value"
    }, {
        "name": "another name",
        "value": "another value"
    }]
}

So the json not maps directly to an entity, in fact I have no model or object for this. What would be the better way to work with it? I mean how to receive and parse the json. I'm new with web api and rest services, and I would appreciate you can help me and explain me with good details. Thanks guys.

Edit:

  1. Any idea of the POCO or class that match this kind of json. ("rule" list It's variable, can be one or more).

  2. After create this poco or class I would have to make a controller based on this?

1
0
5/25/2016 9:59:06 PM

Accepted Answer

As others have said, what you need is a POCO object to represent your request. Based on the information you have provided the following should achieve close enough to what you are after:

public enum CrudAction
{
    Create,
    Read,
    Update,
    Delete
}

public sealed class CrudRequestHeader
{
    public CrudAction Action { get; set; }

    public string Object { get; set; }

    public string User { get; set; }
}

public sealed class RuleDefinition
{
    public string Name { get; set; }

    public string Value { get; set; }
}

public sealed class CrudRequest
{
    public CrudRequestHeader Head { get; set;}

    public Dictionary<string, string> Object { get; set; }

    public List<RuleDefinition> Rule { get; set; }
}

In your Web API controller method you can then take a parameter of type CrudRequest and your JSON will be deserialized to this object, e.g:

public IHttpActionResult Post(CrudRequest crudRequest)
{
    // TODO Implementation
}

You may notice I have used Dictionary<string, string> for CrudRequest.Object as it is variable how many key/values we will be supplied with, I have made the assumption that all values are strings, you can use an object value if you prefer but you will then need to handle the type of value. In the same principle I have used List<RuleDefinition> for CrudRequest.Rule to cater for the variable number of rules which may be supplied.

A LINQPad sample containing the above definitions and use with your input can be found here: http://share.linqpad.net/7rvmhh.linq

4
5/25/2016 10:28:05 PM

Popular Answer

The best thing to do would be to create a class that models the object you expect back.

This way in your Web API method you can use the [FromBody] attribute to automatically parse the body of the request.

Example -

Your data contract would look like this:

public class MyContract
{
  public string MyData { get; set;} 
}

In your ApiController

[HttpPost]
[Route("api/myobject")]
public async Task ReceiveMyObject([FromBody]MyContract object) {

  var data = object.MyData;
  // Do whatever you need to do here.
}

This may seem tedious but this will let you keep your code organized.

Edit So to create a contract out of this:

{
    "head": {
        "action": "create",
        "object": "oneobject",
        "user": "theuser"
   },
    "object": {
        "name1": "a name 1",
        "name2": "a name 2",
        "description": "a description here"
    },
    "rule": [{
        "name": "any name",
        "value": "any value"
    }, {
        "name": "another name",
        "value": "another value"
    }]
} 

You would do something like this:

public class MyContract 
{
    [JsonProperty("head")]
    public MetaObject Head 
    {
        get; set;
    }

    // Not sure if this will work, but it probably will
    [JsonProperty("object")]
    public JObject ExtendedInformation
    {
        get;
        set;
    }

    [JsonProperty("rule")]
    public Rule[] Rules 
    {
        get;
        set;
    }
}

// "MetaObject" definition omitted but you can understand my point with the below

public class Rule
{
    [JsonProperty("name")]
    public string Name
    {
        get;
        set;
    }

    [JsonProperty("value")]
    public string Value
    {
        get;
        set;
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow