简体   繁体   中英

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. I have a 2 part question.

The 1st one is dealing with outputting the JSON data:

            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. Right now the above Response.Write(json) returns 238 separate json strings:

{"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.

{ "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 ?

The second question might be a direct result of how I'm currently outputting the JSON data from the first question. Ultimately I want to be able to read what ever I output from question 1 using this function. Right now my I set my dataType: "html" as that is the only thing I could get to return anything. However, that gives me a length for my msg of 32000+... Something isn't right.

What do I have to do to be able to read the JSON data output from my ASPX page?

    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.

This is probably because the data type is HTML, which is probably necessary because it's not seeing the response as correctly formatted 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 . 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. This should allow you to set the data type:

dataType: "json"

Which should, in turn, give you what you're looking for in 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. 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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