为什么我不能使用我的JSON数据?

时间:2015-02-11 20:13:37

标签: javascript c# asp.net sql-server json

我正在尝试从AJAX函数中读取一些JSON数据。这是我正在使用的AJAX:

             $.ajax({
                type: 'POST',
                cache: false,
                url: 'Default.aspx/GetCoords',
                data: "{}",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    var text = JSON.stringify(data);
                    $("#jsonData").html(text);
                    alert(text);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });

我也使用JSON.parse代替stringify,对象返回undefined或者根本不返回。这来自C#函数,如下所示:

    [WebMethod]
    [ScriptMethod]
    public static string GetCoords()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM DRAW ORDER BY DrawID DESC;", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row;
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                System.Diagnostics.Debug.WriteLine(serializer.Serialize(rows));
                return serializer.Serialize(rows);                
            }
        }
    }

返回JSON数据:以下是调试器控制台上的内容以及当我在段落标记中调用该函数时:

[{"DrawID":925,"xCoord":466,"yCoord":201},{"DrawID":924,"xCoord":385,"yCoord":318},{"DrawID":923,"xCoord":768,"yCoord":159},{"DrawID":922,"xCoord":543,"yCoord":214},{"DrawID":921,"xCoord":329,"yCoord":172}]

但是,当我将它发送到JQuery中的段落标记或在HTML段落中调用它时,它的外观如下:

{"d":"[{\"DrawID\":925,\"xCoord\":466,\"yCoord\":201},{\"DrawID\":924,\"xCoord\":385,\"yCoord\":318},{\"DrawID\":923,\"xCoord\":768,\"yCoord\":159},{\"DrawID\":922,\"xCoord\":543,\"yCoord\":214},{\"DrawID\":921,\"xCoord\":329,\"yCoord\":172}]"}

我有两个问题: (1)为什么我无法使用此数据将xCoords和yCoords插入画布?我尝试了很多格式:(data.d [0] .xCoord,d [0] .xCoord,text.d [0] .xCoord,text [0] .xCoord,等等)。 (2)上面的第二种格式是否会在尝试提取画布上绘画的数据时出现问题(它是我想知道的反斜杠)?

感谢。

4 个答案:

答案 0 :(得分:2)

在你的成功函数中,你没有stringify或json.Parse'data'变量,而是data.d变量。 'd'是您需要解析的json返回的成员。

所以,将var text = JSON.stringify(data);改为var text = JSON.stringify(data.d);,你应该是金色的。

答案 1 :(得分:1)

您需要使用parseJSON,因此:

myThing = {"d":"[{\"DrawID\":925,\"xCoord\":466,\"yCoord\":201},{\"DrawID\":924,\"xCoord\":385,\"yCoord\":318},{\"DrawID\":923,\"xCoord\":768,\"yCoord\":159},{\"DrawID\":922,\"xCoord\":543,\"yCoord\":214},{\"DrawID\":921,\"xCoord\":329,\"yCoord\":172}]"};

myJSON = $.parseJSON(myThing.d);
alert (myJSON[0].DrawID); //etc

答案 2 :(得分:0)

[WebMethod]
[ScriptMethod]
public static Dictionary<string, object>[] GetCoords()
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM DRAW ORDER BY DrawID DESC;", con))
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            System.Diagnostics.Debug.WriteLine(serializer.Serialize(rows));
            return rows.ToArray();                
        }
    }
}

Java脚本:

         $.ajax({
            type: 'POST',
            cache: false,
            url: 'Default.aspx/GetCoords',
            data: "{}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {

                $.each(data, function(index, value) {
                    var x = value.xCoord;
                    /// do something with it ;)
                });
                var text = JSON.stringify(data);
                $("#jsonData").html(text);
                alert(text);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });

答案 3 :(得分:0)

我必须在我的C#代码中返回行,因为@lujcon说:

[WebMethod]
[ScriptMethod]
public static List<Dictionary<string, object>> GetCoords()
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM DRAW ORDER BY DrawID DESC;", con))
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            System.Diagnostics.Debug.WriteLine(serializer.Serialize(rows));
            return rows;                
        }
    }
}

然后我必须将我的var text = JSON.stringify(data);更改为var text = JSON.stringify(data.d);,因为@JustinRusso说。现在工作。谢谢,大家。