简体   繁体   中英

Why is JSON Object for Ajax throwing an error, but json file doesn't?

I try to give a JSON object to display in a google chart.

I can show a google chart when i call in the ajax the json file, but when i try to get the data from codebehind, i get an error.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetData()
    {

        var chartData = new object[10+ 1];
        int j = 0;
        for (int i=1; i <= 10; i++)
        {

            chartData[j] = new object[] { i.ToString(), i*i, i};
            j++;
        }
        //return chartData;
        string json = JsonConvert.SerializeObject(chartData);
             return json;
     }

The Json string i get here is valid according to a json formatter. "[[\"1\",1,1],[\"2\",4,2],[\"3\",9,3],[\"4\",16,4],[\"5\",25,5],[\"6\",36,6],[\"7\",49,7],[\"8\",64,8],[\"9\",81,9],[\"10\",100,10]]"

So i have string, number, number as an array.

My call looks like this (I have it from https://www.encodedna.com/google-chart/create-line-charts-with-dynamic-json-data-using-google-charts.htm ) and this works fine if i use a json file for the data, when i try to use the data from code behind it jumps into the error:

<script>
// Visualization API with the 'corechart' package.  url: "data.json",
google.charts.load('visualization', { packages: ['corechart'] });
google.charts.setOnLoadCallback(drawLineChart);

function drawLineChart() {
    $.ajax({
        url: "Chart.aspx/GetData",
        dataType: "json",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var arrSales = [['RejectReason', 'Count', 'Perc. (%)']];    // Define an array and assign columns for the chart.

            // Loop through each data and populate the array.
            $.each(data, function (index, value) {
                arrSales.push([value.RejectReason, value.Count, value.Perc]);
            });

            // Set chart Options.
            var options = {
                title: 'Sorting',
                curveType: 'function',
                legend: { position: 'bottom', textStyle: { color: '#555', fontSize: 14 } }  // You can position the legend on 'top' or at the 'bottom'.
            };

            // Create DataTable and add the array to it.
            var figures = google.visualization.arrayToDataTable(arrSales)

            // Define the chart type (LineChart) and the container (a DIV in our case).
            var chart = new google.visualization.LineChart(document.getElementById('chart'));
            chart.draw(figures, options);      // Draw the chart with Options.
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('Got an Error');
        }
    });
}

I want the code behind to be shown in the google chart. Later i want to use extracted and enriched data from a database to be shown, so i will not access it from a json file.

This is wrong JSON data

You should avoid the " and \ from your JSON string

[[1,1,1],[2,4,2],[3,9,3],[4,16,4],[5,25,5],[6,36,6],[7,49,7],[8,64,8],[9,81,9],[10,100,10]]

you can use the copied code from JSON formatter, it is just made for view purposes only. Your JSON file contain correct JSON. i think it is not a human-readable format.

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