【问题标题】:returning json path in controller MVC在控制器 MVC 中返回 json 路径
【发布时间】:2017-08-26 00:51:26
【问题描述】:

我有一个返回 json 路径的存储过程:

GO

alter PROCEDURE [dbo].[usp_getjsondata](


@type  INT                 

)
AS
BEGIN

SELECT
          [Time] as 'Time',
          cast([Value] as varchar(10)) as 'Value'
      FROM [dbo].[tbl_data] where type = @type
      for JSON PATH

END

存储过程返回以下内容:

在控制器中我编写了以下代码:

var json = entities.Database.SqlQuery("exec usp_getjsondata  @type",
               new SqlParameter("@type", type)
        ).ToList();

json 数据未存储在变量 json 中。 我在这里遗漏了什么吗?

【问题讨论】:

  • 向我们展示存储过程的完整定义,特别是它的参数是如何定义的。
  • @Amy 我更新了我的问题
  • 尝试从您的代码中删除单词“exec”。只是猜测,但这可能是问题所在。
  • 我需要 exec 来执行存储过程。返回结果
  • 从 .Net 使用时不需要它。 entities.Database.SqlQuery("usp_getjsondata...

标签: c# json entity-framework


【解决方案1】:

为任何对此感到困惑的人发布我的答案,

FOR JSON 将返回一个带有 nvarchar(max) 字段的单条记录,该字段包含 JSON 格式的结果。

string json = string.Concat(entities.Database.SqlQuery("exec usp_getjsondata  @type",
    new SqlParameter("@type", type)
));

您可以将其转换回对象列表,然后您需要解析 JSON:

var results = JsonConvert.DeserializeObject<IList<Class>>(json);

但是,如果要这样做,最好从存储过程中删除 FOR JSON,并将结果直接加载到列表中:

var results = entities.Database.SqlQuery<Class>("exec usp_getdata @type",
    new SqlParameter("@type", type)
).ToList();

【讨论】:

    【解决方案2】:

    我一直在寻找帮助初学者了解此解决方案并帮助更多人解决此问题的人。这是一个抽象工厂的完整解决方案,以帮助轻松使用 FOR JSON PATH(我希望)。

    namespace DB {
        public class DbFactory
        {
            public static T ExecForJson<T>(string query, params object[] parans) where T : new()
            {
                var obj = new T();
    
                using (var db_ = new DbEntities())
                {
    
                    obj = ExecForJson<T>(db_, query, parans);}
                
                
                return obj;
            }
            
            public static T ExecForJson<T>(DbEntities db_, string query, params object[] parans) where T : new()
            {
                var json = string.Concat(db_.Database.SqlQuery<string>(query, parans).ToList());
    
                if (string.IsNullOrEmpty(json)) return new T();
    
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            }
    
            public static System.Data.SqlClient.SqlParameter Param(string name, object value)
            {
                return new System.Data.SqlClient.SqlParameter(name, value);
            }
        }
    }
    

    这样使用:

    var id = 123;
    //like this (create new instance)
    var itens = DB.DbFactory.ExecForJson<List<Item>>("SELECT  * FROM Items WHERE ID = @ID FOR JSON PATH;", DB.DbFactory.Param("@ID", id));
    
    //or like this (with current instance)
    var itens = DB.DbFactory.ExecForJson<List<Item>>(db, "SELECT  * FROM Items WHERE ID = @ID FOR JSON PATH;", DB.DbFactory.Param("@ID", id))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-17
      • 2014-11-16
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多