如何在c#中查看或读取JSON对象的内容

本文关键字:JSON 读取 对象 | 更新日期: 2023-09-27 18:11:59

我正在做一个使用数据生成"Highcharts"图的项目。基本上,它只是一个x/y线序列图,但图表使用了错误的x轴数据。我想弄清楚为什么会这样。我拥有的代码片段如下:

var data = this.costAnalyzerService.OpenCostWindow(DataToSend, CurrentTrain, CustId);
var obj = Json(data, JsonRequestBehavior.AllowGet);
return obj;

data变量类型为Systems.Collections.Generic.List<System.Tuple<int, double, double>>

基本上,这个变量中的所有数据都是正确的。整型是x轴,两个双精度是两组线序列数据。我有理由相信生成绘图的代码没有使用"int"作为x轴。相反,它只是简单地使用1,2,3…,N个点为x轴数。

我如何窥视这个"JSON"对象,看看发生了什么,它返回什么。如果我通过这一点逐步执行代码,则只生成图表,应用程序的执行就完成了。在此之后,没有更多的步骤点需要调试。从字面上看,该函数返回JSON对象并结束。据我所知,它甚至不会返回到调用函数。

下面是我正在使用的COMPLETE方法:

 public JsonResult PlotCostAnalyzerChart(double acidPrice = 0.0, double causticPrice = 0.0, int acidUsage = 0, int causticUsage = 0, int cationResin = 0, int anionResin = 0, bool loadOnSettingsUpdate = false)
    {
        try
        {
            PriceData DataToSend = this.Session["Data_ToSend"] != null ? Session["Data_ToSend"] as PriceData : new PriceData();
            string CustId = this.Session["CustomerId"] != null ? Session["CustomerId"].ToString() : string.Empty;
            if (loadOnSettingsUpdate)
            {
                DataToSend.AcidPrice = acidPrice;
                DataToSend.CausticPrice = causticPrice;
                DataToSend.AcidUsage = acidUsage;
                DataToSend.CausticUsage = causticUsage;
                DataToSend.AmountCation = cationResin;
                DataToSend.AmountAnion = anionResin;
            }
            else
            {
                DataToSend.AcidUsage = 6;
                DataToSend.CausticUsage = 6;
                DataToSend.AmountCation = 600;
                DataToSend.AmountAnion = 600;
            }
            int CurrentTrain = 1;//set the current train in scope
            //if (Session["SelectedTrain"]!=null)
            //{
            //    CurrentTrain = int.Parse(Session["SelectedTrain"].ToString());
            //}
            var data = this.costAnalyzerService.OpenCostWindow(DataToSend, CurrentTrain, CustId);
            var obj = Json(data, JsonRequestBehavior.AllowGet);
            return obj;
        }
        catch
        {
            throw;
        }
    }

非常感谢输入,建议和建议!

这里是一些java脚本,我认为是生成情节:

$(function () {
$.ajax({
    url: 'PlotCostAnalyzerChart',
    type: "GET",
    dataType: "json",
    success: function (jsonData) {
        $.ajax({
            url: 'GetCumulativeSavings',
            type: 'GET',
            success: function (CumulativeSavingsData) {
                $("#CumulativeSavings").empty();
                $("#CumulativeSavings").html(CumulativeSavingsData);
            },
            error: function (xhr) {
                window.location.href = "/ClientDatabase/Errorview";
            }
        });
        var arrxaxis = new Array();
        var arrWithOutClean = new Array();
        var arrWithClean = new Array();
        for (var i = 0 ; i < jsonData.length ; i++) {
            var mxaxis = new Object();
            var mWithOutClean = new Object();
            var mWithClean = new Object();
            mxaxis = jsonData[i];
            mWithOutClean = jsonData[i];
            mWithClean = jsonData[i];
            arrxaxis.push(mxaxis.Item1);
            arrWithOutClean.push(mWithOutClean.Item2);
            arrWithClean.push(mWithClean.Item3);
        }
        //alert(arr);
        $('#graph_CostAnalyzer').empty();
        $('#graph_CostAnalyzer').highcharts({
            chart: {
                zoomType: 'x'
            },
            title: {
                text: 'Cost Analyzer',
                x: -20 //center
            },
            xAxis: {
                title: {
                    text: 'Number of Weeks'
                }
            },
            yAxis: {
                title: {
                    text: 'Cost of Operations'
                }
            },
            credits: {
                enabled: false
            },
            tooltip: {
                crosshairs: true,
                formatter: function () {
                    return 'Week Number : ' + this.point.x + '<br>' + this.series.name + ' : ' + Highcharts.numberFormat(this.point.y, 2) + '<br>' + 'Click on chart to get cost analysis';
                }
            },
            plotOptions: {
                series: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function (e) {      
                          $("#CostAnalyzerResultsTable").fadeOut(300);
                                $.ajax({
                                    url: 'GetResultsTable',
                                    type: "GET",
                                    data: { weekNumber: this.x },
                                    success: function (CostAnalyzerResultsData) {
                                        //alert(CostAnalyzerResultsData);
                                        $("#CostAnalyzerResultsTable").empty();
                                        $("#CostAnalyzerResultsTable").html(CostAnalyzerResultsData);
                                        //FADEOUT AND FADEIN           
                                        $("#CostAnalyzerResultsTable").fadeIn(400);
                                    }
                                });
                            }
                        }
                    },
                    marker: {
                        lineWidth: 1
                    }
                }
            },
            legend:{
                enabled: true,
                layout: 'horizontal',
                borderWidth: 1
            },                
            series: [{
                name: 'With RTI Cleaning',
                data: arrWithClean
            },
            {
                name: 'Without RTI Cleaning',
                data: arrWithOutClean
            }]
        });
    }
});

});

如何在c#中查看或读取JSON对象的内容

好了,我把它砍了下来,终于回答了我自己的问题。谢谢你的鼓励!答案在于修改javascript。我从来没有使用过js,所以这是第一次。

下面是正在运行的代码:

$(function () {
$.ajax({
    url: 'PlotCostAnalyzerChart',
    type: "GET",
    dataType: "json",
    success: function (jsonData) {
        $.ajax({
            url: 'GetCumulativeSavings',
            type: 'GET',
            success: function (CumulativeSavingsData) {
                $("#CumulativeSavings").empty();
                $("#CumulativeSavings").html(CumulativeSavingsData);
            },
            error: function (xhr) {
                window.location.href = "/ClientDatabase/Errorview";
            }
        });
        var arrxaxis = new Array();
        var arrWithOutClean = new Array();
        var arrWithClean = new Array();
        for (var i = 0 ; i < jsonData.length ; i++) {
            var mxaxis = new Object();
            var mWithOutClean = new Object();
            var mWithClean = new Object();
            mxaxis = jsonData[i];
            mWithOutClean = jsonData[i];
            mWithClean = jsonData[i];
            arrxaxis.push(mxaxis.Item1);
            arrWithOutClean.push(mWithOutClean.Item2);
            arrWithClean.push(mWithClean.Item3);
        }
        //alert(arr);
        $('#graph_CostAnalyzer').empty();
        $('#graph_CostAnalyzer').highcharts({
            chart: {
                zoomType: 'x'
            },
            title: {
                text: 'Cost Analyzer',
                x: -20 //center
            },
            xAxis: {
                title: {
                    text: 'Number of Weeks'
                }
            },
            yAxis: {
                title: {
                    text: 'Cost of Operations'
                }
            },
            credits: {
                enabled: false
            },
            tooltip: {
                crosshairs: true,
                formatter: function () {
                    return 'Week Number : ' + this.point.x + '<br>' + this.series.name + ' : ' + Highcharts.numberFormat(this.point.y, 2) + '<br>' + 'Click on chart to get cost analysis';
                }
            },
            plotOptions: {
                series: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function (e) {      
                          $("#CostAnalyzerResultsTable").fadeOut(300);
                                $.ajax({
                                    url: 'GetResultsTable',
                                    type: "GET",
                                    data: { weekNumber: this.x },
                                    success: function (CostAnalyzerResultsData) {
                                        //alert(CostAnalyzerResultsData);
                                        $("#CostAnalyzerResultsTable").empty();
                                        $("#CostAnalyzerResultsTable").html(CostAnalyzerResultsData);
                                        //FADEOUT AND FADEIN           
                                        $("#CostAnalyzerResultsTable").fadeIn(400);
                                    }
                                });
                            }
                        }
                    },
                    marker: {
                        lineWidth: 1
                    }
                }
            },
            legend:{
                enabled: true,
                layout: 'horizontal',
                borderWidth: 1
            },                
            series: [{
                name: 'With RTI Cleaning',
                data: arrWithClean,
                pointStart: arrxaxis[0]
            },
            {
                name: 'Without RTI Cleaning',
                data: arrWithOutClean,
                pointStart: arrxaxis[0]
            }]
        });
        }
    });
});

让我解决这个问题的细节是var arrxaxis = new Array()变量的添加。然后我推了这个变量,让我可以访问它。然后,我简单地添加了pointStart: arrxaxis[0],以在序列中给出适当的起始点:代码的[{}]部分。希望有一天能帮助到像我这样的人。最好,约翰。