简体   繁体   English

从ASPX Web表单读取JSON数据

[英]reading JSON data from aspx web form

I have an Regular ASP.Net web form (not a web service) that I'm trying to spit out JSON data which I'm then trying to consume. 我有一个普通的ASP.Net Web表单(不是Web服务),我试图吐出JSON数据,然后尝试使用。 I have a 2 part question. 我有一个两部分的问题。

The 1st one is dealing with outputting the JSON data: 第一个是处理JSON数据的输出:

            var reader2 = command.ExecuteReader();
            while (reader2.Read())
            {
                var json = Json.Encode(new{
                    code = reader2[1].ToString(),
                    Revenue = reader2[4].ToString()
                });

                Response.Write(json);
            }

reader2 contains 238 different entries. reader2包含238个不同的条目。 Right now the above Response.Write(json) returns 238 separate json strings: 现在,上面的Response.Write(json)返回238个单独的json字符串:

{"code":"123","Revenue":"90.0000"}{"code":"234","Revenue":"90.0000"}

I think it might be helpful later (for question 2) if I had them grouped into 1 recordset. 我认为如果将它们分组为1个记录集,以后(对于问题2)可能会有所帮助。

{ "records": [ { "code":"123" , "Revenue":"90.0000" }, { "code":"234" , "Revenue":"90.0000" } ] } {“ records”:[{“ code”:“ 123”,“ Revenue”:“ 90.0000”},{“ code”:“ 234”,“ Revenue”:“ 90.0000”}]}

How would I do that with the above snippet using the reader and the System.Web.Helpers.Json ? 我如何使用阅读器和System.Web.Helpers.Json使用上面的代码段执行此操作?

The second question might be a direct result of how I'm currently outputting the JSON data from the first question. 第二个问题可能是我当前如何从第一个问题输出JSON数据的直接结果。 Ultimately I want to be able to read what ever I output from question 1 using this function. 最终,我希望能够使用此功能读取问题1的输出。 Right now my I set my dataType: "html" as that is the only thing I could get to return anything. 现在,我将我的dataType: "html"设置为dataType: "html"因为这是我返回任何东西的唯一方法。 However, that gives me a length for my msg of 32000+... Something isn't right. 但是,这使我的msg长度达到了msg

What do I have to do to be able to read the JSON data output from my ASPX page? 为了能够从ASPX页面读取JSON数据输出,我该怎么做?

    function populateResults(code, district, year) {
        $.ajax({
            type: "GET",
            url: "regular.aspx",
            data: "code=" + code + "year=" + year,
            dataType: "html",
            success: function (msg) {
                var results = msg;

                $.each(results, function (index, result) {
                    console.log(result.code);
                });
            }
        });

Re-reading the question after my comment, it's a little more clear now that "32000" isn't too much data in general, it's just that the data is being treated as one big string instead of as an array of objects. 在评论后重新阅读该问题,现在,“ 32000”通常不是太多数据 ,这更加清楚了,只是将数据视为一个大字符串而不是对象数组。

This is probably because the data type is HTML, which is probably necessary because it's not seeing the response as correctly formatted JSON. 这可能是因为数据类型是HTML,这可能是必需的,因为它没有将响应视为正确格式的JSON。 I think you're right that it needs to be "grouped" though it might not need to be wrapped in a parent object like that. 我认为您是正确的,尽管可能不需要将其包装在这样的父对象中,但它需要“分组”。 Try prepending with a [ , appending with a ] , and separating each with a comma. 尝试在[之前加上,在]前面加上每个逗号。 Maybe something like this: 也许是这样的:

var reader2 = command.ExecuteReader();

Response.Write("[");
var firstRecord = true;

while (reader2.Read())
{
    if (!firstRecord)
        Response.Write(",");
    firstRecord = false;

    var json = Json.Encode(new{
        code = reader2[1].ToString(),
        Revenue = reader2[4].ToString()
    });
    Response.Write(json);
}

Response.Write("]");

I had to throw in a little logic there to determine when to include a comma, since you don't want one before the first record or after the last record. 我不得不在其中添加一些逻辑来确定何时添加逗号,因为您不希望在第一条记录之前或最后一条记录之后添加逗号。 There may be a more elegant way to do that, especially if you know the count coming from reader2 . 这样做可能会有更优雅的方法,尤其是当您知道来自reader2的计数时。 But hopefully you get the idea. 但希望您能明白。

This should result in something more like this: 这应该导致更像这样的事情:

[{"code":"123","Revenue":"90.0000"},{"code":"234","Revenue":"90.0000"}]

Which by itself should be interpretable as JSON by the browser. 浏览器本身应将其解释为JSON。 This should allow you to set the data type: 这应该允许您设置数据类型:

dataType: "json"

Which should, in turn, give you what you're looking for in msg . 哪一个应该反过来给您在msg寻找的东西。

Edit: You may be able to simplify this a little more by turning the output from reader2 into an enumeration of objects and just using Json.Encode() on that whole thing. 编辑:通过将reader2的输出转换为对象的枚举,并在整个过程中仅使用Json.Encode() ,您也许可以进一步简化此操作。 Maybe something like: 也许像这样:

var records = new List<CustomRecord>();

while (reader2.Read())
{
    records.Add(new CustomRecord {
        code = reader2[1].ToString(),
        Revenue = reader2[4].ToString()
    });
}

Response.Write(Json.Encode(records));

And have a simple custom object: 并有一个简单的自定义对象:

class CustomRecord
{
    public string code { get; set; }
    public string Revenue { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM