解析Json .Net Web Api [英] Parsing Json .Net Web Api

查看:120
本文介绍了解析Json .Net Web Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Web API 2 / Entity Framework 6项目,我正在做REST服务,但是对于一个特定的服务,我将通过Post(通过Post)接收一个JSON,然后对任何实体进行任何CRUD操作该模型(必须对数据进行一些业务验证,添加或补充某些事物并决定实体保存),JSON是:

  {
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
$,b


$ b $ / code>

所以json不直接映射到一个实体,实际上我没有模型或对象。什么是更好的方式来处理它?我的意思是如何接收和解析json。我是新的web api和休息的服务,我很感激你可以帮助我解释我与良好的细节。谢谢你们。



编辑:


  1. 或类似这种json的类。 (规则列表它是变量,可以是一个或多个)。


  2. 创建此poco或类后,我将必须基于此创建一个控制器?



解决方案

正如其他人所说,你需要的是一个POCO对象代表您的要求根据您提供的信息,以下内容应该足够接近您以后的内容:

  public enum CrudAction 
{
创建,
阅读,
更新,
删除
}

公开封闭类CrudRequestHeader
{
public CrudAction Action {get;组; }

public string Object {get;组; }

public string User {get;组; }
}

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

public string Value {get;组;
}

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

public Dictionary< string,string>对象{get;组; }

public List< RuleDefinition>规则{get;组; }
}

在您的Web API控制器方法中,您可以使用 CrudRequest ,您的JSON将被反序列化为此对象,例如:

  public IHttpActionResult Post(CrudRequest crudRequest)
{
// TODO实现
}

您可能会注意到,对于 CrudRequest.Object ,我使用了字典< string,string> ,因为它是可变的我们将提供许多键/值,我假定所有值都是字符串,如果您愿意,可以使用对象值,但是您将需要处理价值的类型。按照相同的原则,我为 CrudRequest.Rule 使用列表< RuleDefinition> 来满足可变数量的规则可以提供。



可以在这里找到包含以上定义和使用的LINQPad样本: http://share.linqpad.net/7rvmhh.linq


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?

解决方案

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

这篇关于解析Json .Net Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆