0

我在将JSON数据与Kendo UI网格绑定时遇到了困难,但是在寻找解决这个问题的方法时,我遇到了另一个问题。正如@Petur Subev在这里提到的,如何正确解析 JSON 以匹配其正确格式。

我当前的 JSON 格式如下:

//{"No":null,"Desc":"asfasfasfasfasfasfasfasfasfasfasfasf","Date":"2013-03-27T00:00:00","Height":0,"Final":null}
//{"No":null,"Desc":"etwetwetwetwet","Date":"2013-03-27T00:00:00","Height":0,"Final":0}

但正如指出的那样,它应该是这样的:

[{"No":null,"Desc":"asfasfasfasfasfasfasfasfasfasfasfasf","Date":"2013-03-27T00:00:00","Height":0,"Final":null},
{"No":null,"Desc":"etwetwetwetwet","Date":"2013-03-27T00:00:00","Height":0,"Final":0},
{"No":null,"Desc":"asfasfasfskfjklajsfkjasklfjklasjfklajsfkljaklsfjklasjfkljasfkljlasf","Date":"2013-03-27T00:00:00","Height":0,"Final":0}]

很难理解为什么会这样,因为我正在使用预构建函数来创建 JSON,但我仍然弄错了,请帮助。

Model Code我有:

public object GetResult(string id)
{
    var sqlCom = new SqlCommand("SELECT [No],[Desc],[Date],[Height],[Final] FROM [cr_form] WHERE [uId]=@id;", sqlConn);
    sqlCom.Parameters.AddWithValue("@id", id);

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    JsonWriter jsonWriter = new JsonTextWriter(sw);
    var rcrds = GETSQLRESULTS(sqlCom);

    try
    {
        int i = 0;
        if (rcrds != null || rcrds.HasRows)
        {
            //jsonWriter.WriteStartObject();
            while (rcrds.Read())
            {
                jsonWriter.WriteStartObject(); //Changed
                for (int j = 0; j < rcrds.FieldCount; j++)
                {
                    jsonWriter.WritePropertyName(rcrds.GetName(j)); // column name
                    jsonWriter.WriteValue(rcrds.GetValue(j)); // value in column
                }
                i++;
                jsonWriter.WriteEndObject(); //Changed
            }
            //jsonWriter.WriteEndObject();

        }

    }

    catch (Exception ex) { }
    return jsonWriter;
}

Controller

public ActionResult GetRecords()
{
    var usrObj = new User();
    var jsnRslt = usrObj.GetResult(Session["Id"].ToString());

//After Changes in the Model I am getting it in the required Array format:
//{"No":null,"Desc":"asfasfasfasfasfasfasfasfasfasfasfasf","Date":"2013-03-27T00:00:00","Height":0,"Final":null}
//{"No":null,"Desc":"etwetwetwetwet","Date":"2013-03-27T00:00:00","Height":0,"Final":0}
//{"No":null,"Des...

    return Json(jsnRslt, JsonRequestBehavior.AllowGet);            
}

添加后WriteStartArray();

{[{"No":null,"Desc":"asfasfasfasfasfasfasfasfasfasfasfasf","Date":"2013-03-27T00:00:00","Height":0,"Final":null},
{"No":null,"Desc":"etwetwetwetwet","Date":"2013-03-27T00:00:00","Height":0,"Final":0},
{"No":null,"Desc":"asfasfasfskfjklajsfkjasklfjklasjfklajsfkljaklsfjklasjfkljasfkljlasf","Date":"2013-03-27T00:00:00","Height":0,"Final":0},
{"No":null,"Desc":"hdfhdfhdfh","Date":"2013-04-04T00:00:00","Height":1,"Final":0}]}
4

2 回答 2

4

为什么要在 GetResult 方法中构建 JSON?为什么要编写这样的管道代码而不是将其留给框架?

你应该关注的是你的业务逻辑。

只需定义一个模型来表示您要返回的数据(您可能需要根据数据库中的列类型调整模型属性的类型):

public class MyModel
{
    public int? No { get; set; }
    public string Desc { get; set; }
    public DateTime Date { get; set; }
    public int? Height { get; set; }
    public int? Final { get; set; }
}

然后让您的方法返回此模型(更准确地说,实际上是此模型的集合):

public IEnumerable<MyModel> GetResult(string id)
{
    string connectionString = "...";
    using (var conn = new SqlConnection(connectionString))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT [No],[Desc],[Date],[Height],[Final] FROM [cr_form] WHERE [uId]=@id;";
        cmd.Parameters.AddWithValue("@id", id);
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return new MyModel
                {
                    No = GetValue(reader, "No"),
                    Desc = reader.GetString(reader.GetOrdinal("Desc")),
                    Date = reader.GetDateTime(reader.GetOrdinal("Date")),
                    Height = GetValue(reader, "Height"),
                    Final = GetValue(reader, "Final"),
                };
            }
        }
    }
}

private static int? GetValue(DbDataReader reader, string columnName)
{
    var columnIndex = reader.GetOrdinal(columnName);
    if (!reader.IsDBNull(columnIndex))
    {
        return reader.GetInt32(columnIndex);
    }
    return null;
}

请注意我是如何删除 tr/catch 逻辑的,因为您在 catch 语句中没有做任何有用的事情,并且默默地使用这样的异常是一个坏主意。

最后在您的控制器操作中简单地:

public ActionResult GetRecords()
{
    var usrObj = new User();
    var jsnRslt = usrObj.GetResult(Session["Id"].ToString());
    return Json(jsnRslt, JsonRequestBehavior.AllowGet);
}

正如您在此示例中看到的,您不必担心管道和 JSON。您应该使用强类型模型并让框架为您执行序列化。

于 2013-04-01T09:32:04.263 回答
1

看起来你只是想WriteStartArray在一开始就打电话......

示例代码:

using System;
using Newtonsoft.Json;

class Test
{
    static void Main()
    {
        using (var writer = new JsonTextWriter(Console.Out))
        {
            writer.WriteStartArray();
            for (int i = 0; i < 5; i++)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("Foo");
                writer.WriteValue(i);
                writer.WriteEnd();
            }
            writer.WriteEnd();
        }
    }
}

输出:

[{"Foo":0},{"Foo":1},{"Foo":2},{"Foo":3},{"Foo":4}]
于 2013-04-01T09:26:05.327 回答