数组中将多余的方括号添加到JSON输出

时间:2019-03-17 14:06:44

标签: c# arrays json

我发现,将数组转换为JSON时,会有多余的方括号阻止数据被解析。

我进行一个AJAX调用,以准备数据服务器端。 C#处理数据并将其转换为数组以供Google图表处理。

这都是动态的,所以我不知道会有多少个数组,因此我无法对这些数组进行硬编码,并且由于性能令人震惊,因此无法循环客户端。

我认为JSON中的JavaScript解析器不是问题,而是我编写的C#中的问题。

我添加了两个JSON链接,一个是输出,另一个是预期的输出。

我的C#是:

public List<object[]> CreateJsonMultiChartData(string serial, string guid, string datefrom, string dateto)
        {
            var serials = _context.RawData.Where(x => x.ProjectGuid == guid).Select(x => x.SerialNumber).Distinct().ToList();
            var projectUid = guid;
            var from = datefrom;
            var to = dateto;

            //Method to return data
            var g = new GetChartDataSprocs(_context, _configuration);

            List<object> chartData = new List<object>();
            var arrays = new List<object[]>();

            foreach (var s in serials)
            {
                var data = g.GetChartDataFromSqlServer(s, projectUid, from, to);               

                foreach (var item in data)
                {
                    var year = Convert.ToDateTime(item.ReadingDate).Year;
                    var month = Convert.ToDateTime(item.ReadingDate).Month;
                    var day = Convert.ToDateTime(item.ReadingDate).Day;
                    var hour = Convert.ToDateTime(item.ReadingDate).Hour;
                    var minute = Convert.ToDateTime(item.ReadingDate).Minute;
                    var second = Convert.ToDateTime(item.ReadingDate).Second;

                    var date = "new Date(" + year + "," + month + "," + day + "," + hour + "," + minute + "," + second + ")";

                    chartData.Add(
                        new ChartTestModel
                        {                            
                            ReadingDate = date,
                            ReadingValue = item.ReadingValue
                        });                    
                };

                arrays.Add(new[] { chartData });
            }

            return arrays;           
        }

我的AJAX响应是:

 public JsonResult GetMultiChartData(string serial, string uid, string from, string to)
        {
            var dateFrom = Convert.ToDateTime(from).ToString("yyyy-MM-dd HH:mm:ss");
            var dateTo = Convert.ToDateTime(to).AddMinutes(1).ToString("yyyy-MM-dd HH:mm:ss");

            var g = new GetChartData(_context, _configuration);
            var items = g.CreateJsonMultiChartData(serial, uid, dateFrom, dateTo);
            //var dsItems = JsonConvert.DeserializeObject(items);

           // return items;
            return Json(JsonConvert.SerializeObject(items, Formatting.Indented));
        }

我的AJAX呼叫是:

 $.ajax({
        url: 'ProjectCharts/GetMultiChartData',
        datatype: 'json',
        type: 'get',
        async: false,
        data: { section: section, uid: uid, from: from, to: to },
        contentType: 'application/json; charset=utf-8',
        success: function (d) {
            parsedData = $.parseJSON(d);               
            var data = new google.visualization.arrayToDataTable([
                { label: 'Date', id: 'ReadingDate', type: 'date' }, // Use object notation to explicitly specify the data type.
                { label: 'Data', id: 'ReadingValue', type: 'number' }, [parsedData]]);              
            var chart = new google.visualization.LineChart(document.getElementById('chart'));
            var options = multiLineChartOptions();
            chart.draw(data, options);
        },
        error: function () {
            alert("Error");
        }
    });

输出在:

Current JSON Ouput

预期输出为: Expected JSON Output

0 个答案:

没有答案